[Israel.pm] perl arithmetic

Jason Friedman jason.friedman at weizmann.ac.il
Sun Apr 18 08:25:08 PDT 2004

The problem with

perl -e 'print int 100*4.56, "\n";'

is the use of the function int.

int takes the integer part and drops the rest
(ie it rounds down to the nearest integer (if it is positive), or up to 
the next integer if it is negative)
that means that 455.999999999 which may be the answer to this problem 
using floating point arithmetic will become 455. The answer is not to use 
the function int.

In this case the solution is simply to drop the int.

perl -e 'print 100*4.56, "\n";'

gives me the right answer. There are also packages on CPAN specifically 
for dealing with currency to prevent these sort of issues.


> sorry guys, but on my perl 5.8.1:
> perl -e 'print int 100*4.56, "\n";'
> gives me:
> 455

More information about the Perl mailing list