develooper Front page | perl.perl5.porters | Postings from February 2003

Re: [perl #20827] Unexpected scientific notation.

Thread Previous
From:
John Peacock
Date:
February 11, 2003 13:33
Subject:
Re: [perl #20827] Unexpected scientific notation.
Message ID:
3E4969F1.3050502@rowman.com
abigail@abigail.nl (via RT) wrote:
>     $ perl -wle '$x = 1597009560; print $x ** 2'
>     2.5504395347313936e+18

$ perl -wl -MMath::BigInt=:constant -e '$x = 1597009560; print $x ** 2'
2550439534731393600

Just because Perl guessed right one time and not the other doesn't mean that you 
should rely on consistent large number behavior.  There is always going to be 
some point where NV's will happen when you would expect UV or IV's to be used. 
If you care about the correctness of your math, use M::BI.

I think I can explain why Perl made the choices it did (and I can even look at 
the source code to see if I am right ;~), but I cannot believe that this should 
be considered to be a bug.  I suspect the multiplication is attempted first 
using the IV's for speed, falling back to the NV's if the IV's overflow.  The 
exponential goes straight for the jugular and uses NV's preferrentially, since 
the odds of overflowing the IV slot are so much greater.

John

-- 
John Peacock
Director of Information Research and Technology
Rowman & Littlefield Publishing Group
4501 Forbes Boulevard
Suite H
Lanham, MD  20706
301-459-3366 x.5010
fax 301-429-5748


Thread Previous


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