-3

console.log(308.22 + 297.93);

In chrome console output is 606.1500000000001 but it should be 606.15. Why?

hackal
  • 15
  • 3
  • 8

2 Answers2

2

Real numbers can not be represented in javascript (and other languages which use a floating-point type) accurately.

They are just an approximation. See floating point for more information.

vsz
  • 4,811
  • 7
  • 41
  • 78
0

It is called rounding error - http://en.wikipedia.org/wiki/Round-off_error

It is down to how computers store floating point numbers.

That is why it is also tricky for comparisons.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127