This article mainly introduces the PHP floating point number comparison method, which has a good reference value. Let’s take a look at it with the editor
The problem of precision of floating point operations
First look at an example:
<?php $a = 0.1; $b = 0.9; $c = 1; var_dump(($a+$b)==$c); var_dump(($c-$b)==$a); ?>
$a $b==$c returns true, correct
$c-$b==$a returns false, error
Why is this so?
After the operation, the actual returned content when the precision is 20 digits is as follows:
<?php $a = 0.1; $b = 0.9; $c = 1; printf("%.20f", $a+$b); // 1.00000000000000000000 printf("%.20f", $c-$b); // 0.09999999999999997780 ?>
$c-$b is 0.09999999999999997780, so comparing with 0.1 returns false
This problem occurs because floating point number calculation involves precision. When floating point numbers are converted to binary, precision may be caused lost.
Floating point number conversion method
The integer part is divided by 2 to take the remainderMethod The decimal part adopts
multiplied by 2 to round method
For example: convert the number 8.5 to binary
The integer part is 8 8/2=4 8%2=0
4/2=2 4%2=02/2=1 2%2=0
1 is smaller than 2, so there is no need to calculate it. The binary representation of the integer 8 is
The decimal part is 0.5 0.5x2 = 1.0
Because the decimal part is 0 after rounding, there is no need to calculate anymore
Decimal The binary representation of 0.5 is 0.1
The binary representation of 8.5is1000.1
Calculate the binary representation of the number 0.90.9x2=1.8
0.8x2=1.60.6x2=1.2
0.2x2=0.4
0.4x2=0.8
0.8x2=1.6
…. After that, the loop continues. When the interception accuracy is N, the number after N will be rounded off, resulting in loss of accuracy.
In the above example, the precision of 0.9 is lost when converted to binary, resulting in errors during comparison.
So never believe that a floating point number is accurate to the last digit, and never compare two floating point numbers for equality.
How to correctly compare floating point numbers1. Use the round method to process and then compareExample:
<?php $a = 0.1; $b = 0.9; $c = 1; var_dump(($c-$b)==$a); // false var_dump(round(($c-$b),1)==round($a,1)); // true ?>
When performing calculations first, use high-precision calculation methods to ensure that accuracy is not lost.
The method of high-precision operation is as follows:bcadd
Combine two high-precision numbers Addition
bccompCompares two high-precision numbers and returns -1,0,1
bcpDivide two high-precision numbers
bcmodFind the remainder of a high-precision number
bcmulMultiply two high-precision numbers
bcpowFind the power of a high-precision number
bcpowmodFind the modulus of the power of high-precision numbers
bcscaleConfigure the default number of decimal points, which is equivalent to "scale="## in Linux bc #bcsqrt
Find the square root of a high-precision numberbcsub
Subtract two high-precision numbersExample:
<?php $a = 0.1; $b = 0.9; $c = 1; var_dump(($c-$b)==$a); // false var_dump(bcsub($c, $b, 1)==$a); // true ?>
##Related recommendations:
Detailed explanation of JS
##javascript implementation to find the square root of Floating point numberDetailed explanation
Sharing examples of solving floating point number inaccurate calculations in PHP
The above is the detailed content of Detailed graphic explanation of PHP floating point number comparison method. For more information, please follow other related articles on the PHP Chinese website!