Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

attoparsec incorrect parsing of doubles

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.

like image 515
user2149177 Avatar asked Nov 28 '22 21:11

user2149177


1 Answers

It is intentional:

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.

attoparsec traded accuracy for speed by default, if you need perfectly accurate parsing, you have to use the slower rationalparser.

The difference in the results is so small that for most purposes, it doesn't matter, so the faster default parser is on the whole probably a bigger win.

like image 80
Daniel Fischer Avatar answered Dec 15 '22 04:12

Daniel Fischer