Probably silly floating comparison question

Nicholas Clark nick at ccl4.org
Tue Aug 8 10:33:28 BST 2006


On Tue, Aug 08, 2006 at 10:04:43AM +0100, london.pm at welikegoats.com wrote:
> 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?

Loop from 0 to 21 (as integers), and display $i/10

Nicholas Clark


More information about the london.pm mailing list