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
Asked
Active
Viewed 3,468 times
0

Goalie Long
- 198
- 3
- 7
2 Answers
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
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