0

I have just tried a very simple calculation on javascript like this: 8.87 + 7.1, but the output is so unpredictable: 15.969999999999999
Can anyone explain why ? And how to prevent this ?
I am using Chrome to execute the calculcation

Goalie Long
  • 198
  • 3
  • 7

2 Answers2

1

Binary numbers can not store floating point values 100% accurate. For this reason, inaccuracies like yours occur. Have a look at the following references:

Floating point inaccuracy examples

What causes floating point rounding errors?

What Every Computer Scientist Should Know About Floating-Point Arithmetic

Community
  • 1
  • 1
T3 H40
  • 2,326
  • 8
  • 32
  • 43
0

Float calculation is precision than integer. It has some error. The best way to prevent it is not using float.

chensunn
  • 155
  • 1
  • 2
  • 8