Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: floating points and sprintf

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


in reply to floating points and sprintf

Basically, print $t produces 14720 when sprintf produces 14719.

My guess: printf will round off to get what you expected, while sprintf %d does an int() which simply truncates. If the value were actually 14719.999999999999997, then that would explain the difference.

Note that floating point binary can't hold 1/10 exactly any more than decimal can hold 1/3 exactly.

Try using %.15f on such a value and see closer too what's really stored.

Replies are listed 'Best First'.
Re: Re: floating points and sprintf
by kennethwlangley (Novice) on Nov 14, 2002 at 15:46 UTC
    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.
      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://212867]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2022-08-19 07:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?