Mysterious calculation error when multiply by 100

后端 未结 2 1340
时光取名叫无心
时光取名叫无心 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: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.

提交回复
热议问题