develooper Front page | perl.perl5.porters | Postings from November 2010

[perl #41202] text->float conversion gives wrong answer

From:
Father Chrysostomos via RT
Date:
November 28, 2010 14:44
Subject:
[perl #41202] text->float conversion gives wrong answer
Message ID:
rt-3.6.HEAD-13564-1290984268-46.41202-15-0@perl.org
On Sun Jan 07 10:45:52 2007, zefram@fysh.org wrote:
> $ perl -lwe 'printf "%f\n", 1180591620717411303424.0'
> 1180591620717411172352.000000
> $
> 
> The value that I put in the source is exactly 2^70.

Perl’s atof implementation (c2988b20) multiplies by ten every time it
scans a new digit. This results in loss of precision. What is the best
way to tell whether an NV differs from the PV it was derived from?
Running it through sprintf internally seems like a bit much.




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