Merge pull request #1036 from paulfd/exponents

Read exponents in floats
This commit is contained in:
Paul Ferrand 2021-11-20 21:59:46 +01:00 committed by GitHub
commit e12ac8bda3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 67 additions and 0 deletions

View file

@ -179,6 +179,17 @@ bool readLeadingFloat(absl::string_view input, F* result, absl::string_view* res
++numberEnd;
}
if (numberEnd < input.size() && input[numberEnd] == 'e') {
++numberEnd;
if (numberEnd < input.size() &&
(input[numberEnd] == '+' || input[numberEnd] == '-' || absl::ascii_isdigit(input[numberEnd])))
++numberEnd;
while (numberEnd < input.size() && absl::ascii_isdigit(input[numberEnd]))
++numberEnd;
}
if (!readFloat(input.substr(0, numberEnd), result))
return false;

View file

@ -498,6 +498,62 @@ TEST_CASE("[Opcode] opcode read (float)")
REQUIRE( opcode.read(spec) == 10.5f );
}
SECTION("Exponential notation")
{
Opcode opcode { "", "1.05e+001" };
OpcodeSpec<float> spec { 0.0f, Range<float>(0.0f, 100.0f), kEnforceLowerBound };
REQUIRE( opcode.read(spec) == 10.5f );
}
SECTION("Exponential notation")
{
Opcode opcode { "", "1.05e+1" };
OpcodeSpec<float> spec { 0.0f, Range<float>(0.0f, 100.0f), kEnforceLowerBound };
REQUIRE( opcode.read(spec) == 10.5f );
}
SECTION("Exponential notation")
{
Opcode opcode { "", "1.05e1" };
OpcodeSpec<float> spec { 0.0f, Range<float>(0.0f, 100.0f), kEnforceLowerBound };
REQUIRE( opcode.read(spec) == 10.5f );
}
SECTION("Exponential notation")
{
Opcode opcode { "", "1e1" };
OpcodeSpec<float> spec { 0.0f, Range<float>(0.0f, 100.0f), kEnforceLowerBound };
REQUIRE( opcode.read(spec) == 10.0f );
}
SECTION("Exponential notation")
{
Opcode opcode { "", "1.5e-001" };
OpcodeSpec<float> spec { 0.0f, Range<float>(0.0f, 100.0f), kEnforceLowerBound };
REQUIRE( opcode.read(spec) == 0.15f );
}
SECTION("Exponential notation")
{
Opcode opcode { "", "1.5e-1" };
OpcodeSpec<float> spec { 0.0f, Range<float>(0.0f, 100.0f), kEnforceLowerBound };
REQUIRE( opcode.read(spec) == 0.15f );
}
SECTION("Exponential notation")
{
Opcode opcode { "", "1e-1" };
OpcodeSpec<float> spec { 0.0f, Range<float>(0.0f, 100.0f), kEnforceLowerBound };
REQUIRE( opcode.read(spec) == 0.1f );
}
SECTION("Exponential notation")
{
Opcode opcode { "", "6.02385e-009" };
OpcodeSpec<float> spec { 0.0f, Range<float>(0.0f, 100.0f), kEnforceLowerBound };
REQUIRE( opcode.read(spec) == 6.02385e-9f );
}
SECTION("Text before")
{
Opcode opcode { "", "garbage10" };