js double precision floating point

First, how to transfer data to a floating-point   https://www.zhihu.com/question/21711083

7707397-a94799e9e1732a9e.png

Two, js's Number

In JavaScript are all integer and floating point data type Number, all numbers are stored as 64-bit floating-point number, true even integer.


7707397-3feabe7229b2336c.png

Third, what caused the problem?

1, the fractional loss of accuracy, such is not equal to 0.1 + 0.2 0.3

2, the maximum range of integer

It is an integer of 54 to the maximum by the maximum count (253 - 1, Number.MAX_SAFE_INTEGER, 9007199254740991) and minimum (- (253 - 1), Number.MIN_SAFE_INTEGER, -9007199254740991) safe range of integers. So as long as more than this range, it will be discarded there is a problem of accuracy.


Fourth, the solution

Open source library, bigInt,

0.1+0.2-0.3     // 5.551115123125783e-17

5.551115123125783e-17.toFixed(20)      //   '0.00000000000000005551'

5.551115123125783e-17<Number.EPSILON*Math.pow(2,2)    // true

Reproduced in: https: //www.jianshu.com/p/e071e1da8dfd

Guess you like

Origin blog.csdn.net/weixin_34082789/article/details/91060349