APM: Perl zero not being zero, but 7e-12

Bill Raty bill.raty at gmail.com
Mon Sep 12 23:06:23 PDT 2005


Remember that even though you initialized $a with 38071.63, the internal 
number is stored in binary, not BCD, and .63 is an irrational number as far 
as binary is concerned. It just all depends on where your perl was compiled, 
what math libraries are being used, and how they represent their floating 
point numbers.

Alternatives:

1) If you're after two base-ten digits of precision you could mult by 100, 
round up, then compare. This is covered in section 2.3.3 of Perl Cookbook, 
2nd edition.

2) Look at some of the better known perl math classes (Math::BigInt and 
Math::BigFloat come to mind).

-Bill



On 9/13/05, HCoyote <hcoyote at ghostar.ath.cx> wrote:
> 
> On Mon, Sep 12, 2005 at 10:46:23AM -0500, David Bluestein II wrote:
> > Okay, I've seen this before and have a question how to avoid it.
> >
> > I take two variables:
> >
> > $a= 38071.63;
> > $b = $i + $j; # Where $i + $j => 38071.63
> >
> > print "$a : $b"; # results in 38071.63 : 38071.63
> >
> > Yet if I do:
> >
> > if ($a == $b) {
> > print "Equal";
> > } else {
> > print "Not equal";
> > }
> >
> > I get a "Not equal".
> >
> > If I subtract ($b-$a) the result is 7.27595761418343e-12. How do I get
> > Perl to ignore this "noise" which comes from someplace?
> 
> I suggest a new computer. :-)
> 
> I can't get this to do the same thing on any system I test on. Does it 
> happen only one
> one system for you or is it consistently the same number? Does it change 
> depending on
> your choice of $i and $j?
> 
> 
> Travis
> _______________________________________________
> Austin mailing list
> Austin at pm.org
> http://mail.pm.org/mailman/listinfo/austin
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/austin/attachments/20050913/7b8f1347/attachment.html


More information about the Austin mailing list