General discussion

Locked

javascript - decimal point

By Anderson S ·
Why JavaScrit is doing this, when I use calculations with decimal point?

Look:
4.4 + .4 = 4.800000000000001 -- right(4.
3.3 + .3 = 3.5999999999999996 -- right(3.6)
6.6 + .6 = 7.199999999999999 -- right(7.2)

code:
num1 = parseFloat(4.4) +parseFloat(.4);

If there are something that I can do, please send me.

Thanks
Anderson

This conversation is currently closed to new comments.

1 total post (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

floating point math

by Jay Eckles In reply to javascript - decimal poi ...

You're running into the limitations of floating point arithmetic. The basic issue is that just as 1/3 cannot be represented in base 10 decimal notation, it can be approximated by .3, .33, .3333, etc. Similarly, it happens that a fraction like 0.1 in base 10 cannot be represented in binary, but it can be approximated. The result of this approximation is what you see in your floating point arithmetic results.

Depending on the level of complexity you're looking for in an explanation, here are some links:

IEEE FP spec
http://www.psc.edu/general/software/packages/ieee/ieee.html

Explanation of FP in Python (but applicable to all languages using FP arithmetic) - well written
http://www.python.org/doc/current/tut/node14.html

Jay

Back to Web Development Forum
1 total post (Page 1 of 1)  

Related Discussions

Related Forums