This article is divided into three parts:
1, JS digital precision loss of some typical problems
2, the reason for the loss of JS digital precision
3. Solutions (one object + one function)
First, JS Digital precision loss of some typical problems
1. Two simple floating-point numbers added
0.1 0.2 0.3 // true
Look at the results of Java operations
2. Large integer operation
9999999999999999 10000000000000001 // true var 9007199254740992 1// true
The 16-and 17-digit numbers are equal, plus 1 are equal.
3. ToFixed will not be rounded (Chrome)
1.335. toFixed (2// 1.33
There has been an inconsistency between the price in Chrome and other browsers on the line because of the toFixed compatibility issue.
second, JS digital loss of precision reasons
The binary implementation of the computer and the number of bits limit cannot be expressed in finite numbers. Just like some irrational numbers can not be limited to express, such as Pi 3.1415926...,1.3333 ... such as JS follows the IEEE 754 specification, using double-precision storage (double precision), occupying a single bit.
A summary of JavaScript digital accuracy loss