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

Divide bug?

Thread Next
From:
Larry Shatzer
Date:
March 11, 2003 11:02
Subject:
Divide bug?
Message ID:
20030311120133.A1949@zippy.zyx.net
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";
}

__END__

-- Larry

Thread Next


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