2019-06-03js floating point precision issues

Often encountered in js + 0.1 0.2! 0.3 == case, this situation is due to floating point calculation results obtained have accuracy problems,

Computer storage data is all the data is stored in binary files, but there are some decimal can not be accurately expressed in binary, so will cause accuracy problems in floating-point calculation

For a computer, the time between two numbers by adding the binary, the result will only show the addition is completed in decimal form, js the number is indicated by the double-precision IEEE 754 64-bit

0.1 converts into binary process

0.1 * 2 = 0.2 - - - - - - - - - - taking 0

0.2 * 2 = 0.4 - - - - - - - - - - taking 0

0.4 * 2 = 0.8 - - - - - - - - - - taking 0

0.8 * 2 = 1.6 - - - - - - - - - - Take 1

0.6 * 2 = 1.2 - - - - - - - - - - Take 1

0.2 * 2 = 0.4 - - - - - - - - - - taking 0

----------------

This will count to find the fractional part and then how to continue to take are not equal to 0 , so binary is no way an accurate representation of 0.1 .

How result of this behavior, mainly in the following ways

Converting decimal integers, and the result appear as a decimal

Reproduced in: https: //www.jianshu.com/p/6f44313abd92

Guess you like

Origin blog.csdn.net/weixin_33912638/article/details/91050496