First look at a piece of code :? Php $ a0.1; $ b0.7; var_dump ($ a + $ B) 0.8); the printed value is booleanfalse. Why? The PHP manual has the following Warning message for floating point numbers: the precision of the Warning floating point number is obviously a simple decimal score, like 0.1 or 0.7. It cannot be converted to internal without losing a little precision.
First look at a piece of code :? Php $ a = 0.1; $ B = 0.7; var_dump ($ a + $ B) = 0.8); the printed value is boolean false. Why? The PHP manual has the following Warning message for floating point numbers: the precision of the Warning floating point number is obviously a simple decimal score, like 0.1 or 0.7. It cannot be converted to internal without losing a little precision.
First, let's look at a piece of code:
$ A = 0.1;
$ B = 0.7;
Var_dump ($ a + $ B) = 0.8 );
The printed value is boolean false.
Why? The PHP Manual provides the following warning information for floating point numbers:
Warning
Floating point Precision
Obviously, a simple decimal score is like 0.1 or 0.7. It cannot be converted to an internal binary format without losing a little precision. This will lead to chaotic results: for example, floor (0.1 + 0.7) * 10) usually returns 7 instead of 8 in expectation, because the internal representation of the result is similar to 7. 9999999999 ....
This is related to the fact that it is impossible to accurately express certain decimal scores with limited digits. For example, decimal 1/3 is changed to 0.3333333 ....
Therefore, never believe that the result of a floating point number is accurate to the last digit, or compare whether the two floating points are equal. If higher precision is required, use any precision mathematical function or gmp function.
Then we should rewrite the formula above
$a = 0.1;
$b = 0.7;
var_dump(bcadd($a,$b,2) == 0.8);
This solves the problem of floating point calculation.