Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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.

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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2020-11-26 21:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?