This question already has answers here:
Closed 7 years ago.
Possible Duplicate:
Is JavaScript’s Floating-Point Math Broken?
In Javascript,
3 * 0.1 = 0.30000000000000004
I think this is due to the language's number system where 0.3 cannot be accurately represented. But why the following?
0.15 * 2 = 0.3
Similarly,
0.1 + 0.2 = 0.30000000000000004
But
0.15 + 0.15 = 0.3
How's so?
But why the following?
0.15 * 2 = 0.3
The result isn't exactly 0.3, but it's close enough so that when the least significant digits are rounded off to display the value, it's rounded to 0.3.
The values 0.1
and 0.15
are not exact either, but the error representing 0.1
seems to be larger than for 0.15
. When you use the values in calculations, the errors accumulate, and sooner or later they become large enough not to be rounded off when the values are displayed.