Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: int() function (updated)

by LanX (Cardinal)
on Oct 24, 2020 at 11:31 UTC ( #11123118=note: print w/replies, xml ) Need Help??

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.


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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11123118]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2021-04-19 22:39 GMT
Find Nodes?
    Voting Booth?

    No recent polls found