The example in this paper describes the two float (floating-point) comparison methods in PHP. Share to everyone for your reference. Specific as follows:
Recently in the development of a contract management system, involving two floating-point comparison, I am depressed miserable.
N long ago, do not know from where to listen to a "Do not use the equal sign to compare floating point" of the "truth", oneself usually also in use, as if there is no problem, but this problem is finally coming.
?
123456789 |
<?php
$sum =
"12300.00"
;
$a
=
"10000.30"
;
$b =
"2000.30"
;
$c
=
"299.40"
;
$sum = (float)
$sum
;
$s = (float) (
$a
+
$b
+
$c
);
var_dump(
$sum
,
$s
);
var_dump(
$sum
==
$s
);
|
The result is:
Float (12300)
Float (12300)
BOOL (FALSE)
Later I know in PHP, to compare the size of two floating-point numbers, you can use Bccomp (parameter 1, parameter 2, decimal place) to compare.
?
123456789 |
<?php
$sum =
"12300.00"
;
$a
=
"10000.30"
;
$b
=
"2000.30"
;
$c
=
"299.40"
;
$sum = (float)
$sum
;
$s = (float) (
$a
+
$b
+
$c
);
var_dump(
$sum
,
$s
);
var_dump(
bccomp
(
$sum
,
$s
,2));
|
Results:
Float (12300)
Float (12300)
Int (0)//0 means two floating-point values are equal
<?php
Echo Bccomp (' 1 ', ' 2 '). "\ n"; -1 less than
echo bccomp (' 1.00001 ', ' 1 ', 3); 0 equals
echo bccomp (' 1.00001 ', ' 1 ', 5); 1 greater than
?>
If you use PHP +-*/to calculate the floating-point number, you may encounter some errors in the calculation results, such as Echo Intval (0.58*100), will print 57 instead of 58
This is actually a bug that the bottom binary of the computer cannot accurately represent floating-point numbers, is a cross-language
can solve problems with the Precision function library
Bcadd-adds two high-precision numbers
Bccomp-compares two high-precision numbers, returns-1, 0, 1
bcdiv-divides two high-precision numbers
Bcmod-seeking high precision digital remainder
bcmul-multiply two high-precision numbers
bcpow-High Precision Digital exponentiation
Bcpowmod-to find high-precision digital exponentiation, number theory is very common
bcscale-Configure the default number of decimal places, equivalent to the "scale=" in the Linux BC
bcsqrt-to find the square root of high precision number
bcsub-to subtract two high-precision numbers
1/** 2 * Two high-precision numbers compared 3 * 4 * @access Global 5 * @param float $left 6 * @param float $right 7 * @param int $sca Le accurate to the number of decimal places 8 * 9 * @return int $left = = $right return 0 | $left < $right back-1 | $left > $right return to */11 var_dump (Bccomp ($left =4.45, $right = 5.54, 2)); 12//113 14/**15 * Two high-precision numbers added 16 * 17 * @access global18 * @param float $left * @param float $right * @param int $scale The exact number of decimal places * * @return String */24 var_dump (Bcadd ($left =1.0321456, $right = 0.0243456, 2)), 25//1.0426 27/**28 * Two high-precision numbers subtracted 29 * 30 * @access global31 * @param float $left @param float $right * @param int $scale The exact number of decimal places * * * @return S Tring */37 var_dump (bcsub ($left =1.0321456, $right = 3.0123456, 2)); 38//-1.9839 40/**41 * Two high-precision numbers divide 42 * 43 * @ Access global44 * @param float $left * @param float $right * @param int $scale The exact number of decimal places * @return St Ring */50 Var_dump (Bcdiv ($left =6, $right = 5, 2)); 51//1.2052 53/**54 * Two high-precision numbers multiplied by * * @access global57 * @param float $left * @param float $right * @param int $scale the exact number of decimal places * @return string */63 var_dump (Bcmul ($left =3.1415926, $right = 2.4569874566, 2)); 64//7.716 5 66/**67 * Set the number of decimal places for the BC function. * * @access global70 * @param int $scale The exact number of decimal places * * @return void 73 * * Bcscale (3); Var_dump (Bcdiv (' 105 ', ' 6.55957 ')); 76//16.007
PHP Floating-point number comparison