Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Re: floating points and sprintf

by kennethwlangley (Novice)
on Nov 14, 2002 at 15:46 UTC ( #212877=note: print w/replies, xml ) Need Help??


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

Check my just posted note. The real value is 147.2. That is mulitplied by 100 (value should be close to 14720.0) and then converted. I understand rounding errors converting from floats to ints but this problem strikes me as a bug.

Replies are listed 'Best First'.
Re: Re: Re: floating points and sprintf
by John M. Dlugosz (Monsignor) on Nov 14, 2002 at 19:11 UTC
    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://212877]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2022-06-30 09:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (97 votes). Check out past polls.

    Notices?