欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

php 精度运算,PHP BC 库(任意精度数字运算) | 网游世界

发布时间:2025/3/8 46 豆豆
生活随笔 收集整理的这篇文章主要介绍了 php 精度运算,PHP BC 库(任意精度数字运算) | 网游世界 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

留意:备选参数$scale以设置运算精度(保留小数位)。

bcscale(设置运算精度)

bool bcscale ( int $scale )

说明:设置运算精度(保留小数位),成功返回TRUE否则为FALSE。

bcadd(加法运算)

string bcadd ( string $left_operand , string $right_operand [, int $scale] )

说明:返回$left_operand+$right_operand的和。

bcsub(减法运算)

string bcsub ( string $left_operand , string $right_operand [, int $scale ] )

返回:$left_operand 减去$right_operand的值。

bcmul(乘法运算)

string bcmul ( string $left_operand , string $right_operand [, int $scale] )

说明:返回$left_operand乘以$right_operand的值。

bcdiv(除法运算)

string bcdiv ( string $left_operand , string $right_operand [, int $scale] )

说明:返回$left_operand除以$right_operand的值。

bccomp(比较运算)

int bccomp ( string $left_operand , string $right_operand [, int $scale] )

说明:返回值为0相等;1,$left_operand大;-1,$right_operand大。

bcmod(取余运算)

string bcmod ( string $left_operand , string $modulus )

说明:返回$left_operand除$modulus的余数。返回 NULL则$modulus为0。

bcpow(次方|幂运算)

string bcpow ( string $left_operand , string $right_operand [, int $scale] )

说明:返回$left_operand的$right_operand次方的幂值。

在不需要精度控制的情况下,将不会返回小数保留位。如<?php

echo bcpow('5', '2', 2); // 显示 "25", 而不是 "25.00"

?>

bcpowmod(次方取余运算)

string bcpowmod ( string $left_operand , string $right_operand , string $modulus [, int $scale] )

说明:返回$left_operand的$right_operand次方,再与 $modulus取余。

即:($left_operand^$right_operand) mod $modulus<?php

$a = bcpowmod($x, $y, $mod);

$b = bcmod(bcpow($x, $y), $mod);

//$a 和 $b 相同。

?>

bcsqrt(平方根运算)

string bcsqrt ( string $operand [, int $scale] )

说明:返回$operand的平方根。

相关:

总结

以上是生活随笔为你收集整理的php 精度运算,PHP BC 库(任意精度数字运算) | 网游世界的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。