Web Development

General discussion


javascript - decimal point

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

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

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

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


This conversation is currently closed to new comments.

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

Explanation of FP in Python (but applicable to all languages using FP arithmetic) - well written


Related Discussions

Related Forums