Mysterious calculation error when multiply by 100

后端 未结 2 1337
时光取名叫无心
时光取名叫无心 2020-12-07 02:21

The following script contains a very strange error. I want to check if a value is a positive integer. To do this, I multiply by 100 to enclose the value to decimal. If I tes

相关标签:
2条回答
  • 2020-12-07 03:01

    That's because javascript casts everything to a double internally. As a result, all calculations pick up some noise due to floating point inaccuracy: Floating point inaccuracy examples

    One way to fix this issue, is to just round down to the nearest int after all intermediate calculations.

    0 讨论(0)
  • 2020-12-07 03:13

    That's called numerical math. Computers do not operate on real numbers but on their approximation and this approximation brings rounding errors such as this one.

    For 0.05, 0.06 and 0.08 you are lucky that these have an exact floating point representation on your PC. This is not the case of 0.07. Note that on a different PC/browser the result might be different.

    For more info see the IEEE floating point format specification.

    0 讨论(0)
提交回复
热议问题