develooper Front page | perl.perl5.porters | Postings from December 2013

Re: [perl #120426] Strange and unwarranted underflow in string-to-number

Thread Previous | Thread Next
From:
Morten Welinder
Date:
December 4, 2013 14:04
Subject:
Re: [perl #120426] Strange and unwarranted underflow in string-to-number
Message ID:
CANv4PNnrsts_r6k56qyGzmXccx2L72gcb=3LT_Y6ukjy5KnGfg@mail.gmail.com
If I understand you right, you are rolling your own atof.  That is tricky
business.  There are some tricky cases at

    http://www.exploringbinary.com/incorrectly-rounded-conversions-in-gcc-and-glibc/

that glibc has only recently gotten right.

M.





On Tue, Dec 3, 2013 at 8:32 AM, Dave Mitchell via RT
<perlbug-followup@perl.org> wrote:
> On Mon, Dec 02, 2013 at 04:34:34PM +0000, Dave Mitchell wrote:
>> On Mon, Dec 02, 2013 at 03:42:59PM +0000, Zefram wrote:
>> > Dave Mitchell wrote:
>> > >    I fixed this by stopping the processing of the integer or fractional
>> > >    component once its been detected that only zeroes are left (with a
>> > >    suitable adjustment of the exponent).
>> >
>> > Eww.  That won't fix cases where the extra digits are non-zero, such as
>> >
>> > $ echo 0.15301e-305 | perl -e '$v = <STDIN>; print "v=", $v + 0, "\n";'
>> > v=0
>> > $ echo 0.15399e-305 | perl -e '$v = <STDIN>; print "v=", $v + 0, "\n";'
>> > v=0
>>
>>
>> Oh yeah :-(
>>
>> I'll need to rethink that.
>
> Second attempt now smoking as smoke-me/davem/float1:
>
> commit 72cfc3028683d7d6428ee0b881a788aba667fe8e
> Author:     David Mitchell <davem@iabyn.com>
> AuthorDate: Mon Dec 2 15:04:49 2013 +0000
> Commit:     David Mitchell <davem@iabyn.com>
> CommitDate: Tue Dec 3 12:30:01 2013 +0000
>
>     [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 par
>     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)
>
>
> --
> Technology is dominated by two types of people: those who understand what
> they do not manage, and those who manage what they do not understand.
>
>

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About