-
Notifications
You must be signed in to change notification settings - Fork 13
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
Float parsing fails the toml-test when using Nim devel #45
Comments
The Here's a minimal reproducible example using import std/[unittest]
import parsetoml # nimble install parsetoml
test "TOML float":
check $("some_float = 0.123".parseString()["some_float"].getFloat()) == "0.123" I dived into the code and ended up in Lines 271 to 299 in 3760867
Here's a minimal example that shows where things are going wrong on devel: import math, sugar
var
invPowerOfTen = 10.0
decimalPart: float64
for ch in ['1', '2', '3']: # 0.123
decimalPart += (int(ch) - int('0')).float / invPowerOfTen
dump ch
dump decimalPart
invPowerOfTen *= 10
doAssert $decimalPart == "0.123" Output:
When I run the same code on latest Nim stable on Playground: https://play.nim-lang.org/#ix=3oMQ, the output is:
These is how far I could get. I don't understand the low level details on why the outcome is different. Also copying @timotheecour in case he's interested in helping fix this in parsetoml. |
OK, so this fails on devel:
I am still not sure if this is something that should be fixed in Nim devel. But assuming it's not, can something like this be done in parsetoml:
Above passes. So we first need to figure out how many decimal point digits we have, scale up the decimal parts and then normalize using a float division. Or somehow |
not just nim devel, 1.4.4 too, and indeed, 0.1 + 0.02 != 0.12 with FP semantics.
|
With the float string representation fixed in Nim devel (See nim-lang/Nim#18139), that uncovered a bug in the logic for parsing float TOML values. For e.g. to parse "foo = 0.123", internally, 0.1 + 0.02 + 0.003 was done which would evaluate to 0.12300000000000001. Earlier float stringification bug caused that value to print out as "0.123" instead of "0.12300000000000001". This is now fixed by using parseutils.parsefloat, which parses the "0.123" float value as 0.123 and not 0.12300000000000001. Fixes NimParsers#45.
With the float string representation fixed in Nim devel (See nim-lang/Nim#18139), that uncovered a bug in the logic for parsing float TOML values. For e.g. to parse "foo = 0.123", internally, 0.1 + 0.02 + 0.003 was done which would evaluate to 0.12300000000000001. Earlier float stringification bug caused that value to print out as "0.123" instead of "0.12300000000000001". This is now fixed by using parseutils.parsefloat, which parses the "0.123" float value as 0.123 and not 0.12300000000000001. Fixes NimParsers#45.
With the float string representation fixed in Nim devel (See nim-lang/Nim#18139), that uncovered a bug in the logic for parsing float TOML values. For e.g. to parse "foo = 0.123", internally, 0.1 + 0.02 + 0.003 was done which would evaluate to 0.12300000000000001. Earlier float stringification bug caused that value to print out as "0.123" instead of "0.12300000000000001". This is now fixed by using parseutils.parsefloat, which parses the "0.123" float value as 0.123 and not 0.12300000000000001. Fixes NimParsers#45.
With the float string representation fixed in Nim devel (See nim-lang/Nim#18139), that uncovered a bug in the logic for parsing float TOML values. For e.g. to parse "foo = 0.123", internally, 0.1 + 0.02 + 0.003 was done which would evaluate to 0.12300000000000001. Earlier float stringification bug caused that value to print out as "0.123" instead of "0.12300000000000001". This is now fixed by using parseutils.parsefloat, which parses the "0.123" float value as 0.123 and not 0.12300000000000001. Fixes #45.
This is the root cause of the recent failure of parsetoml tests on Travis weekly cron: https://travis-ci.org/github/NimParsers/parsetoml/builds/773550319
See nim-lang/Nim#18182 (comment) .
The text was updated successfully, but these errors were encountered: