-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,637
  • 6
  • 37
  • 73
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
  • 57,599
  • 16
  • 82
  • 120