Possible Duplicate:
Is JavaScript's Math broken?
I came across this rounding issue:
When I do this:
.2 + .1 results in 0.30000000000000004
.7 + .1 results in 0.7999999999999999
1.1 + .1 results in 1.2000000000000002
and so on…
Can anyone explain (in detail) why? Probably some binary rounding stuff. But I like to really know what happens…
In a nutshell, because
.2isn’t actually .2; it’s actually the closest representable double-precision number, which isSimilarly,
.1is reallyWhen you add those together, the result is rounded again to the nearest representable number, which is
Finally, when you print it out, that number gets rounded to 17 decimal digits, giving the result you observe.
Your other examples follow the same pattern.