BC is the abbreviation for binary calculator. The arguments to the bc* function are the operands plus an optional [int scale], such as String Bcadd (String $left _operand, String $right _operand[, int $scale]), if scale is not provided , the default value of Bcscale is used. Here the large number is directly represented by a string of 0-9, and the result of the calculation is also a string.
1 Bcadd-Add two high-precision numbers2 Bccomp-Compare two high-precision numbers, return-1, 0, 13 Bcdiv-Divide two high-precision numbers4 Bcmod-High precision digital remainder5 Bcmul-Multiply two high-precision numbers6 Bcpow-High precision digital exponentiation7 Bcpowmod-To find high-precision digital exponentiation, number theory is very common8 Bcscale-Configure the default number of decimal places, equivalent to the "scale=" in Linux BC"9 bcsqrt-Calculate the square root of high precision numberTen bcsub-Subtract two high-precision numbers
First look at a piece of code:
1 <? PHP 2 $a = 0.1; 3 $b = 0.7; 4 Var_dump (($a$b) = = 0.8);
The printed value is actually a Boolean false
Why is this? The PHP manual has the following warning message for floating-point numbers:
Floating point Accuracy
It is obvious that a simple decimal score like 0.1 or 0.7 cannot be converted to an internal binary format without losing a little bit of precision. This can result in confusion: for example, floor ((0.1+0.7) *10 typically returns 7 instead of 8 as expected because the internal representation of the result is actually similar to 7.9999999999 ....
This is related to the fact that it is impossible to accurately express certain decimal fractions with a finite number of digits. For example, the decimal 1/3 becomes 0.3333333. . .。
So never believe that the floating-point number is accurate to the last one, and never compare two floating-point numbers for equality. If you do need a higher precision, you should use any mathematical or GMP function with any precision.
So the above formula should be rewritten as
1 <? PHP 2 $a = 0.1; 3 $b = 0.7; 4 Var_dump (bcadd($a,$b, 2) = = 0.8);
This will solve the problem of floating-point calculation.
The BC series function for precise operation of PHP floating-point numbers