I just sent this post to perl5-porters. This had me pulling my hair out as to what is causing it.
Below is a copy of the email I sent...
A co-worker of mine came across something strange that had me pulling my hair
out. Below is an example of the bug in action. Logically you would think that if
you divide 72.9 by 100, you get 0.729, so it should match the if statement. At
first we thought it was because we had 0.729000 as the condition, but that
doesn't matter. Also in the below code I have it test against 0.729 and 0.729000
without it resulting from a divide, and it passes.
I tested this against 5.005_03, 5.6.0, 5.6.1 and 5.8.0, all exhibit this
behaviour.
#!/usr/bin/perl
use warnings;
use strict;
my $test = 72.9;
my $divtest = $test/100;
my $condition = 0.729;
my $hardcode = 0.729000;
print "divtest => $divtest\ncondition => $condition\n";
if ($divtest == $condition) {
print "Should be true\n";
}
elsif ($divtest == $hardcode) {
print "This also should be true\n";
}
else {
print "What the?!!?\n";
}
if ($condition == $hardcode) {
print "Without divide it works\n";
}
UPDATE: Mark Jason Dominus set me straight, but as I just wrote back, I figured since their NV's are both 0.729 in the output from Devel:Peel, it would work... guess not, someone else replied directly to me and suggested using 'eq', but using that for numerical comparisons makes me feel a bit ill.
Instead of comparing for exact equality, check that they are relatively close. I.e. instead of:
if ( $x == 0.329 )
use:
if( abs($x-0.329) 1e-7 )
Re:floating point numbers are only equal by luck
exeunt on 2003-03-11T20:15:41
I get a syntax error, are you sure you got that right?Re:floating point numbers are only equal by luck
bart on 2003-03-11T21:10:09
The < sign got dropped. This is HTML...
printf("%.20f\n", 72.9/100);
Re:It's not just perl...
exeunt on 2003-03-11T19:58:02
Yeah, MJD sent me another email in private withprintf "%.40f", $f
to see what is going on...:)