PHP floating point precision issues

Using php + - * / float calculation of the time, you may encounter some calculations wrong results, as follows:

? < PHP
 echo  intval (0.58 * 100); // output 57

Solution

. 1 <? PHP
 2  echo  bcmul (0.58, 100); // Output 58

● php provides high accuracy BC libraries deal with this problem

● bcadd - two high-precision digital sum

● bcsub - two high-precision digital subtraction

● bcmul - two high-precision digital multiplication

● bcdiv - two high-precision digital division

● bccomp - Comparison of two high-precision digital, -1, 0, 1

● bcmod - seeking high-precision digital remainder

● bcpow - seeking precision digital power

● bcpowmod - seeking precision digital power modulo very commonly used in number theory

● bcscale - configure the default number of decimal places, the equivalent is Linux bc of the "scale ="

● bcsqrt - seeking precision digital square root

 

Link: https: //www.php.cn/php-weizijiaocheng-435795.html

Guess you like

Origin www.cnblogs.com/clubs/p/11950655.html