This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #120426] atof() small value rounding errors
authorDavid Mitchell <davem@iabyn.com>
Mon, 2 Dec 2013 15:04:49 +0000 (15:04 +0000)
committerDavid Mitchell <davem@iabyn.com>
Wed, 25 Dec 2013 21:40:32 +0000 (21:40 +0000)
commitb27804d8b48d647c08dc853b49e5b311fe166616
tree8ba60a501056223f70bc9dfffa54c8f8752750c4
parentba0f5503397ddc65667224047f121adc8b44784c
[perl #120426] atof() small value rounding errors

For something like 0.153e-305, which is small, but not quite the smallest
number (which is around 2.2e-308), adding extra digits to the fractional part
could cause unnecessary rounding to zero.

From the bug report:

    $ echo 0.1530e-305 | perl -e '$v = <STDIN>; print "v=", $v + 0, "\n";'
    v=0
    $ echo 0.153e-305  | perl -e '$v = <STDIN>; print "v=", $v + 0, "\n";'
    v=1.53e-306

This was because 0.1234e-305 is calculated as

    1234 / (10^309)

and 10^309 becomes infinity. In these edge cases, repeatedly decrement
the exponent and divide the mantissa by 10 until the exponent becomes in
range; in this case we instead calculate

    123 / (10^308)
numeric.c
t/opbasic/arith.t