Php floating point number calculation comparison and integer inaccuracy solution, the integer is not accurate. Php floating point number calculation comparison and integer inaccuracy solution, integer inaccuracy php interesting phenomenon, it should be a phenomenon in many programming languages. This is because the computation of the computer's php floating point is not accurate, and the integer is not accurate.
The interesting phenomenon of php should be that many programming languages have such a phenomenon. This is because the computer itself has a problem with floating point recognition .....
[Php]View plaincopy
- $ F = 0.58;
- Var_dump (intval ($ f * 100*100); // result 5799
- Var_dump (float) ($ f * 100*100); // result 5800
- Echo (int) (0.1 + 0.7) * 10); // Result 7
- Echo (float) (0.1 + 0.7) * 10); // Result 8
[Php]View plaincopy
-
- $ A = 0.1;
- $ B = 0.7;
- Var_dump ($ a + $ B) = 0.8 );
- // The printed value is boolean false.
[Php]View plaincopy
-
- $ A = 0.1;
- $ B = 0.7;
- Var_dump (bcadd ($ a, $ B, 2) = 0.8); // bool true
Do not convert a floating point to an integer when calculating a floating point. Otherwise, an unpredictable error may occur.
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.
The interesting phenomenon in php is that many programming languages have such a phenomenon. This is because the computer itself...