-3

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.

Community
  • 1
  • 1
Aftab
  • 39
  • 1
  • 6

2 Answers2

1

Because you are using floating point arithmetic. Here is an explanation of what is happening.

Lucas Green
  • 3,951
  • 22
  • 27
1

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.

Pointy
  • 405,095
  • 59
  • 585
  • 614