Solution to inaccurate floating-point calculation comparison and rounding in PHP. Inaccurate rounding
An interesting phenomenon in PHP that should be seen in many programming languages. This is due to the problem of the computer’s own recognition of floating point numbers....
[php] view plaincopy
- $f = 0.58;
- var_dump(intval($f * 100 *100));
- var_dump((float)($f * 100 *100));
- echo (int)((0.1 0.7)*10);
- echo (float)((0.1 0.7)*10);
[php] view plaincopy
-
- $a = 0.1;
- $b = 0.7;
- var_dump(($a $b) == 0.8);
[php] view plaincopy
-
- $a = 0.1;
- $b = 0.7;
- var_dump(bcadd($a,$b,2) == 0.8);
When calculating floating point numbers, remember not to convert floating point numbers into integers, otherwise unpredictable errors will occur.
So never trust that a floating point number result is accurate to the last digit, and never compare two floating point numbers to see if they are equal.
http://www.bkjia.com/PHPjc/1040163.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1040163.htmlTechArticlePHP floating point calculation comparison and inaccurate rounding solution, inaccurate php interesting phenomenon should be Many programming languages have this phenomenon. This is because of the computer itself...