Possible Duplicate:
Is JavaScript's Math broken?
Why JS show 25.1+61.7+13.2 = 100.00000000000001 ? Its fixed if you just change the position of the number like 13.2+25.1+61.7 = 100. Can anyone explain this.
Possible Duplicate:
Is JavaScript's Math broken?
Why JS show 25.1+61.7+13.2 = 100.00000000000001 ? Its fixed if you just change the position of the number like 13.2+25.1+61.7 = 100. Can anyone explain this.
Because you are using floating point arithmetic. Here is an explanation of what is happening.
JavaScript uses IEEE 754 floating point math, which is a base-2 floating point format. There are many many values that cannot be represented exactly in that format, and specifically among them are decimal fractions like x.7
.
It's not a compiler issue. It's an issue with the fundamental way that JavaScript represents real numbers. Many other languages would have the exact same problem.
Computers don't handle real numbers in a rigorous way, generally.