The problem of background data loss when the number is too large

Recently, the development project encountered a pit. The data requested by the interface is inconsistent with the data returned by the browser. It was later found that the accuracy was lost due to the excessive number.

The range of the maximum value in java is larger than that in js, so it may cause the problem of precision loss

And lead to inconsistency between preview and response in the browser development tools, the response is the source data, and the preview data is processed, resulting in the loss of precision of excessive numbers, as shown in the figure

 

 The solution is to use string processing in the background.

 

The problem of js precision loss:

Due to the computer's binary implementation and the limitation of the number of bits, some numbers cannot be represented in a limited way. Just like some irrational numbers cannot be expressed finitely, such as pi 3.1415926 ..., 1.3333 ... etc. JS follows the IEEE 754 specification, uses double precision storage (double precision), and occupies 64 bits.

 

(1 bit is used to represent the sign bit, 11 bits are used to represent the exponent, 52 bits are used to represent the mantissa)

Because no matter what kind of expression is used for calculation, to the bottom of the computer, the specific data and operations are realized through the machine code of 1 and 0. Due to the underlying implementation mechanism, when a floating-point number is converted to a binary representation, it cannot accurately represent such data with a decimal point. The essence is to convert the floating-point number to the closest approximate value expressed in binary. The following example can be used to briefly explain the calculation method of floating-point numbers when converted to binary. As shown below:

 

0.02625 = 0.000001101 (binary), the binary representation cannot be obtained accurately, so the "rounding method" is used (every 1 enters, every 0 rounds).

The above is the principle of the problem. Many compiled languages ​​such as Java and c # encapsulate the processing of floating point numbers. Therefore, in most cases, there will be no obvious visible problems. js itself as an interpreted language, it seems that this has a natural disadvantage.


Reference link: https://blog.csdn.net/summerjx/java/article/details/81983459

 

Guess you like

Origin www.cnblogs.com/gopark/p/12693037.html
Recommended