php high precision calculation problem

php high precision calculation problem

I am engaged in the financial industry and have frequent capital calculations. Here are the pits I encountered.... If you are not careful, the user's funds may lose hundreds of thousands, or even more terrible... Let's go directly to the example:

javascript

Why is 0.1 + 0.2 not equal to 0.3? (Correct result: 0.30000000000000004)

Why is 0.8 * 7 not equal to 5.6? (Correct result: 5.6000000000000005)

PHP

var_dump (range (0.58 * 100));

The correct result is 57, not 58

The trouble with floating-point arithmetic

In fact, these results are not bugs of the language, but are related to the implementation principle of the language. All numbers in js are unified as Numbers, including integers, which are actually all double-precision (double) types.

And PHP will distinguish between int and float. No matter what language, as long as floating-point operations are involved, there are similar problems, and you must pay attention when using them.

Note: If you use PHP's +-*/ to calculate floating-point numbers, you may encounter some problems with wrong calculation results, such as the above echo intval( 0.58 * 100 ); will print 57 instead of 58, which is actually a computer A bug that the underlying binary cannot accurately represent floating point numbers is cross-language, and I also encountered this problem with python. So basically most languages ​​provide class libraries or function libraries for accurate calculation. For example, php has BC high-precision function library. I will introduce some commonly used BC high-precision functions later.

Again, back to question 57,58 above.

Why is the output 57? Is it a PHP bug?

  To understand why, first we need to know the representation of floating point numbers (IEEE 754):

  Floating-point numbers, taking 64-bit length (double precision) as an example, will be represented by 1-bit sign bit (E), 11 exponent bits (Q), and 52-bit mantissa (M) (a total of 64 bits).

  Sign bit: The highest bit indicates the positive or negative of the data, 0 indicates a positive number, and 1 indicates a negative number.

  Exponent bit: Indicates that the data is a power of 2, and the exponent is represented by an offset code

  Mantissa: Indicates the significant digits after the decimal point of the data.

  这里的关键点就在于, 小数在二进制的表示, 关于小数如何用二进制表示, 大家可以百度一下, 我这里就不再赘述, 我们关键的要了解, 0.58 对于二进制表示来说, 是无限长的值(下面的数字省掉了隐含的1)..

  0.58的二进制表示基本上(52位)是: 00101000111101011100001010001111010111000010100011110.57的二进制表示基本上(52位)是: 001000111101011100001010001111010111000010100011110而两者的二进制, 如果只是通过这52位计算的话,分别是:www.111cn.net

  0.58 -> 0.579999999999999960.57 -> 0.5699999999999999至于0.58 * 100的具体浮点数乘法, 我们不考虑那么细, 有兴趣的可以看(Floating point), 我们就模糊的以心算来看… 0.58 * 100 = 57.999999999

  那你intval一下, 自然就是57了….

  可见, 这个问题的关键点就是: “你看似有穷的小数, 在计算机的二进制表示里却是无穷的”

  因此, 不要再以为这是PHP的bug了, 这就是这样的…..

  PHP浮点型在进行+-*%/存在不准确的问题

继续看一段代码:

$a = 0.1;
$b = 0.7;
var_dump(($a + $b) == 0.8); // false

打印出来的值为 boolean false

  这是为啥?PHP手册对于浮点数有以下警告信息:

  Warning

  浮点数精度

  显然简单的十进制分数如同 0.1 或 0.7 不能在不丢失一点点精度的情况下转换为内部二进制的格式。这就会造成混乱的结果:例如,floor((0.1+0.7)*10) 通常会返回 7 而不是预期中的 8,因为该结果内部的表示其实是类似 7.9999999999…。

  这和一个事实有关,那就是不可能精确的用有限位数表达某些十进制分数。例如,十进制的 1/3 变成了 0.3333333. . .。

  所以永远不要相信浮点数结果精确到了最后一位,也永远不要比较两个浮点数是否相等。如果确实需要更高的精度,应该使用任意精度数学函数或者 gmp 函数

那么上面的算式我们应该改写为

$a = 0.1;
$b = 0.7;
var_dump(bcadd($a,$b,2) == 0.8); // true

常用的高精度函数如下:

复制代码
  bcadd — 将两个高精度数字相加

  bccomp — 比较两个高精度数字,返回-1, 0, 1

  bcdiv — 将两个高精度数字相除

  bcmod — 求高精度数字余数

  bcmul — 将两个高精度数字相乘

  bcpow — 求高精度数字乘方

  bcpowmod — 求高精度数字乘方求模,数论里非常常用

  bcscale — 配置默认小数点位数,相当于就是Linux bc中的”scale=

  bcsqrt — 求高精度数字平方根

  bcsub — 将两个高精度数字相减
复制代码

 BC高精确度函数库包含了:相加,比较,相除,相减,求余,相乘,n次方,配置默认小数点数目,求平方。这些函数在涉及到有关金钱计算时比较有用,比如电商的价格计算。
复制代码
/**
  * 两个高精度数比较
  * 
  * @access global
  * @param float $left
  * @param float $right
  * @param int $scale 精确到的小数点位数
  * 
  * @return int $left==$right 返回 0 | $left<$right 返回 -1 | $left>$right 返回 1
  */
var_dump(bccomp($left=4.45, $right=5.54, 2));
// -1
  
 /**
  * 两个高精度数相加
  * 
  * @access global
  * @param float $left
  * @param float $right
  * @param int $scale 精确到的小数点位数
  * 
  * @return string 
  */
var_dump(bcadd($left=1.0321456, $right=0.0243456, 2));
//1.05
 
  /**
  * 两个高精度数相减
  * 
  * @access global
  * @param float $left
  * @param float $right
  * @param int $scale 精确到的小数点位数
  * 
  * @return string 
  */
var_dump(bcsub($left=1.0321456, $right=3.0123456, 2));
//-1.98
  
 /**
  * 两个高精度数相除
  * 
  * @access global
  * @param float $left
  * @param float $right
  * @param int $scale 精确到的小数点位数
  * 
  * @return string 
  */
var_dump(bcdiv($left=6, $right=5, 2));
//1.20
 
 /**
  * 两个高精度数相乘
  * 
  * @access global
  * @param float $left
  * @param float $right
  * @param int $scale 精确到的小数点位数
  * 
  * @return string 
  */
var_dump(bcmul($left=3.1415926, $right=2.4569874566, 2));
//7.71
 
 /**
  * 设置bc函数的小数点位数
  * 
  * @access global
  * @param int $scale 精确到的小数点位数
  * 
  * @return void 
  */ 
bcscale(3);
var_dump(bcdiv('105', '6.55957')); 
//php7.1 16
复制代码

这样就可以解决浮点数运算的问题啦....

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324839439&siteId=291194637