http://qs321.pair.com?node_id=11123118


in reply to int() function

Perl does an internal magic error correction/guessing when printing a number like 195.999999...etc to compensate rounding errors on the last digits. (This of course in binary and not decimal representation)

I think that's why it prints $x1 as 196.

I'll check as soon my laptop is up and running.

update

as I thought, it's compensating the rounding error from not being able to accurately represent 0.15 in a based 2 floating number:

DB<28> $x1 = (1.15*170)+0.50; DB<29> printf("%.20f",$x1) 195.99999999999997000000 DB<30> printf("%.20f",0.15) 0.14999999999999999000 DB<31>

There is an easy workaround , avoid the decimal point when calculating and divide at the very end.

DB<35> $x1 = (115*170)+50; # no fractions DB<36> $x1 /=100 DB<37> p "$x1 = ",int $x1 196 = 196 DB<38>

see also Humans have too many fingers

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery