Skip to content

Commit

Permalink
WIP: Let's try to extract this to a separate PR another day.
Browse files Browse the repository at this point in the history
  • Loading branch information
perlun committed Oct 13, 2023
1 parent 2c510e6 commit 9a711c2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
13 changes: 11 additions & 2 deletions src/Perlang.Parser/NumberParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,17 @@ public static INumericLiteral Parse(NumericToken numericToken)
{
// The explicit IFormatProvider is required to ensure we use 123.45 format, regardless of host OS
// language/region settings. See #263 for more details.
float value = Single.Parse(numberCharacters, CultureInfo.InvariantCulture);
return new FloatingPointLiteral<float>(value);
//
// An interesting detail: despite this being a `float` value, we parse it as a double since we
// might otherwise loose valuable precision. This is critical for working with numbers like
// 340282349999999991754788743781432688640.0f; parsing it as a Single at this point will round
// it to 3.4028235E+38 which looses a significant amount of precision. This is particularly
// important to get proper semantics for `double + float` operations.

// TODO: Try to extract this change + changes to BinaryOperatorData to a PR of its own. Should
// work, and help provide smaller, more maintainable PRs.
double value = Double.Parse(numberCharacters, CultureInfo.InvariantCulture);
return new FloatingPointLiteral<double>(value);
}

case 'd':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ public static class BinaryOperatorData
{
new object[] { "12", "34", "false" },
new object[] { "12", "12", "true" },
new object[] { "12", "12.0", "false" }, // Same value but different types. Note: this is truthy in C# AND Java.
new object[] { "12", "12.0", "false" }, // Same value but different types. Note: this is truthy in C# AND Java. AND C++! We want to
new object[] { "12.0", "12", "false" }, // Same value but different types. Note: this is truthy in C# AND Java.
new object[] { "12.345", "12.345", "true" },
new object[] { "12.345", "67.890", "false" },
Expand Down
10 changes: 7 additions & 3 deletions src/stdlib/test/print.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ TEST(PrintDouble_4294967283)

TEST(PrintDouble_9223372036854775807)
{
fwrite_mocked = true;
perlang::print(9223372036854775807.0);
fwrite_mocked = false;
float f = 12.0;
int i = 12;
perlang::print(f == i);

// fwrite_mocked = true;
// perlang::print(9223372036854775807.0);
// fwrite_mocked = false;
}

0 comments on commit 9a711c2

Please sign in to comment.