0

I'm adding 0.5 to a number every second. However, it seems to be adding 0.5000000000001 instead.

window.setInterval(function() {
    number += 0.5;
    // it says number as 0.50000000001, and after 1.00000000002
}, 1000);

Does anybody know why this is happening?

1 Answers1

0

It is a limitation of floating point numbers. They are not that accurate and hence the values that you have obtained.

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