Home > Article > Backend Development > How PHP handles high-precision calculation functions
This article mainly shares with you how PHP handles high-precision calculation functions. It mainly shares with you two methods of graphing. I hope it can help you.
Method 1:
PHP provides a binary calculator (Binary Calculator) for arbitrary precision mathematical calculations, which supports numbers of any size and precision, described in string form
bcadd — Addition
bccomp — Comparison
bcp — Division
bcmod — Find the remainder
bcmul — Multiplication
bcpow — Power
bcpowmod — Power first and then find the remainder
bcscale — Set the decimal place precision for all functions
bcsqrt — Find the square root
bcsub — Subtraction
Method 2:
I encountered a strange problem recently, the mall paid through WeChat The order price is often one penny less. After investigation, it is caused by the accuracy of PHP floating point operations.
It is caused by the accuracy of PHP floating point operations. Brother Niao's Bolg has a detailed explanation. http://www.laruence.com/2013/03/26/2884.html,
When decimals are represented in binary, 0.58 is an infinitely long value for binary.
The binary representation of 0.58 is basically (52 bits) is: 0010100011110101110000101000111101011100001010001111
The binary representation of 0.57 is basically (52 bits): 001000111101011100001010001111010111000 0101000111101
Convert to floating point number (64-bit double precision)
0.58 - > 0.57999999999999996
0.57 -> 0.56999999999999995
0.58*100 = 57.999999999 (int)(0.58*100) = 57
Solution:
(int)((0.58*1000)/10) = 58
Related recommendations:
Explanation of common date and calculation functions in mysql
PHP probability calculation function
php Method to calculate function execution time_PHP tutorial
The above is the detailed content of How PHP handles high-precision calculation functions. For more information, please follow other related articles on the PHP Chinese website!