Attoparsec incorrect doubles parsing

I use attoparsec 'double' and 'number' built-in parsers to parse floating point values, and I get different results from different parsers.

syntax number is "8.918605790440055e-2"

Done 8.918605790440054e-2

> Double parsing "8.918605790440055e-2"

Done "" 8.918605790440055e-2

Using the 'number' parser seems to lose some precision, while the "double" parser does not. How can 8.918605790440055e-2 be represented as double, since the "double" parser manages to do this, why does the "number" parser return a different result? This is mistake?

I am using attoparsec 0.10.4.0.

+4
source share
2 answers

Ok, let's see the documentation .

  • double

    Note. This feature is almost ten times faster than rational , but slightly less accurate.

    The double type supports about 16 decimal places of precision. For 94.2% of numbers, this function and rational give the same results, but for the remaining 5.8%, this function loses accuracy around the 15th decimal place. For 0.001% of numbers, this function will lose accuracy at 13th or 14th decimal places.

  • number

    Note. This feature is almost ten times faster than rational . On integrated inputs, it gives quite accurate answers, and on floating-point inputs it is slightly less accurate than rational.

Both of them are recognized as slightly inaccurate (in exchange for a relatively fast). number inaccuracy is not quantified, so the fact that its inaccuracy does not coincide with double is not an error.

If accuracy is paramount, use rational .

+12
source

This is intentional :

Note. This feature is almost ten times faster than rational. On integrated inputs it gives completely accurate answers and on floating point inputs, it is slightly less accurate than rational .

attoparsec default price is speed, if you need accurate parsing, you should use a slow rational parser.

The difference in results is so small that for most purposes it does not matter, therefore a faster default parser as a whole is probably a big win.

+14
source

All Articles