Problems? Is your data what you think it is? | |
PerlMonks |
Re: Perl 5.8.x floating point representation errorby gone2015 (Deacon) |
on Jan 25, 2009 at 20:32 UTC ( [id://738807]=note: print w/replies, xml ) | Need Help?? |
FWIW: I've looked at the Perl source (in particular numeric.c) and have submitted a bug report. It turns out that Perl has a home-grown Perl_my_atof(), which makes a reasonable fist of converting decimal to binary, given that it is trying to do so in a system-independent manner. To produce the required "correctly rounded" result it is necessary to use some extended precision arithmetic, which is hard to do in a system-independent fashion. The code in Perl_my_atof() handles the digits before and after the decimal point as separate floating point values, which is a form of extended precision -- unfortunately, the way it does this can cause multiple roundings to occur... It goes wrong in in this case because your number 0.99999999976716936 has 17 digits after the decimal point, which is rounded to 99999999976716928 and rounded again when it is divided by 10**17 to give 0.999999999767169245323827908578 (approx). IMHO Perl_my_atof() should be rewritten to use the atof() C library function to do the actual conversion -- having done all the Perl specific parsing and locale and such, at the text level. That way the local library can use the required local means to get the required precision.
In Section
Seekers of Perl Wisdom
|
|