jxb has asked for the wisdom of the Perl Monks concerning the following question:
And here is the output:#!/usr/bin/perl use strict; my @a; $a[0] = (0.1 - 0.1) * 10; #Should be 0 $a[1] = (0.2 - 0.1) * 10; #Should be 1 $a[2] = (0.3 - 0.1) * 10; #Should be 2 $a[3] = (0.4 - 0.1) * 10; #Should be 3 $a[4] = (0.5 - 0.1) * 10; #Should be 4 my $i; for( $i = 0; $i < 5; $i++ ) { print "$i: ", $a[$i], " ", int($a[$i]), "\n"; } print "\n"; for( $i = 0; $i < 5; $i++ ) { printf "%d: %s %d %f\n", $i, $a[$i], $a[$i], $a[$i]; }
On the lines with *s you would expect it to print '2 2' & '2 2 2.000000', but instead it prints '1's. I tested both 'print' & 'printf' here to see if it was an issue with one of those functions alone, or perhaps the data type the $a[]'s were assumed to be. This only seems to happen for the calculation of0: 0 0 1: 1 1 2: 2 1 * 3: 3 3 4: 4 4 0: 0 0 0.000000 1: 1 1 1.000000 2: 2 1 2.000000 * 3: 3 3 3.000000 4: 4 4 4.000000
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: An arithmetic oddity
by ikegami (Patriarch) on Aug 31, 2009 at 20:00 UTC | |
|
Re: An arithmetic oddity
by JavaFan (Canon) on Aug 31, 2009 at 19:58 UTC | |
|
Re: An arithmetic oddity
by bv (Friar) on Aug 31, 2009 at 20:05 UTC | |
|
Re: An arithmetic oddity
by jxb (Initiate) on Sep 01, 2009 at 01:29 UTC |