PHP floating point calculation comparison and rounding inaccurate solution, rounding inaccurate
PHP interesting phenomenon, it should be a lot of programming languages will have this phenomenon. This is because the computer itself on the problem of 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 actually a Boolean false
[PHP]View Plaincopy
-
- $a = 0.1;
- $b = 0.7;
- Var_dump (bcadd ($a,$b, 2) = = 0.8); BOOL True
When calculating floating-point numbers, remember not to convert floating-point numbers into plastic, otherwise there will be errors that cannot be estimated.
So never believe that the floating-point number is accurate to the last one, and never compare two floating-point numbers for equality.
http://www.bkjia.com/PHPjc/1040163.html www.bkjia.com true http://www.bkjia.com/PHPjc/1040163.html techarticle PHP Floating-point calculation comparison and rounding not accurate solution, take the whole inaccurate PHP interesting phenomenon, it should be a lot of programming languages will have this phenomenon. This is because the computer itself ...