Floating Point 0.2 + 0.1 Rounding Error
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.7999999999999
Solution 1:
In a nutshell, because .2
isn't actually .2; it's actually the closest representable double-precision number, which is
0.200000000000000011102230246251565404236316680908203125.
Similarly, .1
is really
0.1000000000000000055511151231257827021181583404541015625
When you add those together, the result is rounded again to the nearest representable number, which is
0.3000000000000000444089209850062616169452667236328125
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.
Post a Comment for "Floating Point 0.2 + 0.1 Rounding Error"