Probably silly floating comparison question

london.pm@welikegoats.com london.pm at welikegoats.com
Tue Aug 8 10:04:43 BST 2006


If we compare the output of:

my $i = 0;
while($i <= 2) {
         $i += .1;
         print"$i\n";
}

with:

my $i = 0;
while($i <= 2) {
         $i += 1;
         print"$i\n";
}

then the first one finishes with 2 and the last one finishes with 3.

I assume this is a rounding error with floating point values. How can I 
make it do what I would expect it to do, which is make the first one 
finish with 2.1?

Regards,
Andrew


More information about the london.pm mailing list