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

Re: 4253.95 + 0.9 = 4254.84999999999 (need help to interpret internals)

by mdillon (Priest)
on Feb 25, 2005 at 10:09 UTC ( [id://434501]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to 4253.95 + 0.9 = 4254.84999999999 (need help to interpret internals)

If you care about your money, use Math::BigFloat, as dragonchild suggested.

As others have said, the problem is that floating point binary can't represent most decimal fractions exactly. It's analogous to the way that 1/3 is a repeating decimal (0.3333...). 0.95 and 0.9 are both repeating binary numbers. As can you see, it goes both ways: even 1/3 in trinary notation does not repeat: 0.1.

The solution is to use "fixed point" or "arbitrary precision" decimal arithmetic. This is usally implemented internally as an integer (or byte array) with a scale. I'm not sure how Math::BigFloat does its job, but it is the standard module for arbitrary precision arithmetic in Perl.

  • Comment on Re: 4253.95 + 0.9 = 4254.84999999999 (need help to interpret internals)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://434501]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.