-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
strutils:formatFloat() gives different result in JS mode #8244
Comments
There are many more cases where formatFloat gives different results on the c/c++ and js backends. import math
import fenv
import strutils
proc echoFloat(f: float) =
echo formatFloat(f, ffDefault)
proc test() =
echoFloat(maximumPositiveValue(float64))
echoFloat(maximumPositiveValue(float32))
echoFloat(minimumPositiveValue(float64))
echoFloat(minimumPositiveValue(float32))
echoFloat(1.0 + epsilon(float64))
echoFloat(1.0 + epsilon(float32))
echoFloat(1.0e23)
echoFloat(log2(100000.0))
echoFloat(PI / 2.0)
echoFloat(PI / 3.0)
echoFloat(E / 2.0)
echoFloat(NaN)
echoFloat(Inf)
echoFloat(-Inf)
echoFloat(0.0)
echoFloat(-0.0)
test() gives
on the c/c++ backends and
on the js backend. Except for the last 5 examples where the correct representation is open to debate, the values printed on the js backend are the correct ones according to the current state of the art in terms of textual floating-point representations. The main problem with the representations on the c/c++ backends (again except for the last 5 examples) is, that you either can't reconstruct the same floating-point value from the textual representation or you are wasting space displaying more digits than are necessary to reconstruct the same floating-point value. |
Does this mean we need different test cases for C/C++ vs JS here or is it reasonable to expect consistent results? |
Personally I would prefer consistent results, but @Araq doesn't seem to care much about these issues (see #7717). If you have some spare time, you could try to wrap https://github.com/google/double-conversion. Then we would have automatically the same behaviour on all backends, as this is the code used in the v8 engine. |
Test case in strutils.nim
Prints "1.234560e+02" in C/C++ mode but prints "1.234560e+2" without the "0" in the exponent in JS mode. Wasn't getting caught all this time since testament wasn't running all test cases. See #8232 and #8242.
The text was updated successfully, but these errors were encountered: