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

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

Father Chrysostomos via RT
November 28, 2010 14:44
[perl #41202] text->float conversion gives wrong answer
Message ID:
On Sun Jan 07 10:45:52 2007, 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. Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About