attoparsec incorrect parsing of doubles
attoparsec, floating-accuracy, floating-point, haskell, parsing
Solution
Well, let's look at the documentation.
`double`
Note: This function is almost ten times faster than `rational`, but is slightly less accurate.
The `Double` type supports about 16 decimal places of accuracy. For 94.2% of numbers, this function and `rational` give identical results, but for the remaining 5.8%, this function loses precision around the 15th decimal place. For 0.001% of numbers, this function will lose precision at the 13th or 14th decimal place.
`number`
Note: This function is almost ten times faster than `rational`. On integral inputs, it gives perfectly accurate answers, and on floating point inputs, it is slightly less accurate than rational.
They are both acknowledged to be slightly inaccurate (in exchange for being relatively fast). `number`'s inaccuracy is not quantified, so the fact that its inaccuracy is not the same as `double`'s is not a bug.
If accuracy is paramount, use `rational`.
Problem
I am using attoparsec's built-in parsers 'double' and 'number' to parse floating point values and I get different results from different parsers. >parse number "8.918605790440055e-2" Done "" 8.918605790440054e-2 > parse double "8.918605790440055e-2" Done "" 8.918605790440055e-2 Using the 'number' parser seems to lose some precision whilst the 'double' parser does not. As it's possible to represent 8.918605790440055e-2 as a double since the 'double' parser manages to do it why does the 'number' parser return a different result? Is this a bug? I am using attoparsec 0.10.4.0.