Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Re: Re: floating points and sprintf

by John M. Dlugosz (Monsignor)
on Nov 14, 2002 at 19:11 UTC ( #212965=note: print w/replies, xml ) Need Help??


in reply to Re: Re: floating points and sprintf
in thread floating points and sprintf

No, the real value is 147.199999999999990, since .2 cannot be represented exactly in a floating-point variable. Multiplying by 100 will not make it magically go back to what you wanted it to be, since the roundoff already occured. 14719.999999... truncated to an int is 14719, exactly what it said it does. This is not a bug, this is a typical floating-point gotcha.

You probably want to correctly round rather than truncating. E.g. $intval= int(0.5 + $floatval);.

—John

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2022-08-19 08:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?