BCMath 任意精度数学

  • 简介
  • 安装/配置
  • BC 数学函数
    • bcadd — 将两个任意精度数字相加
    • bccomp — 比较两个任意精度数字
    • bcdiv — 将两个任意精度数字相除
    • bcmod — 获取任意精度数字的模
    • bcmul — 将两个任意精度数字相乘
    • bcpow — 将任意精度数字提升到另一个数字
    • bcpowmod — 将任意精度数字提升到另一个数字,并通过指定模数进行缩减
    • bcscale — 设置或获取所有 bc 数学函数的默认比例参数
    • bcsqrt — 获取任意精度数字的平方根
    • bcsub — 从另一个任意精度数字中减去一个任意精度数字
添加注释

用户贡献的注释 6 个注释

75
Hayley Watson
8 年前
此扩展是 Philip Nelson 创建的基本计算器实用程序库的 GNU 实现的接口;因此得名。
24
volek at adamv dot cz
9 年前
请注意,当您使用 ClaudiuS 制作的阶乘实现时,即使您尝试计算您通常无法计算的数字的阶乘,例如 2.5、-2 等,您也会得到结果。以下是一个更安全的实现
<?php
/**
* 计算给定数字的阶乘。
* @param string|int $num
* @throws InvalidArgumentException
* @return string
*/
function bcfact($num)
{
if (!
filter_var($num, FILTER_VALIDATE_INT) || $num <= 0) {
throw new
InvalidArgumentException(sprintf('参数必须是自然数,给定 "%s"。', $num));
}

for (
$result = '1'; $num > 0; $num--) {
$result = bcmul($result, $num);
}

return
$result;
}
?>
18
ClaudiuS
11 年前
需要计算一些排列组合,发现 BC 扩展很棒,但在函数方面很弱,所以直到这个函数在这里实现,这里有一个阶乘函数

<?php
/* BC 阶乘
* n! = n * (n-1) * (n-2) .. 1 [例如 5! = 5 * 4 * 3 * 2 * 1 = 120]
*/
function bcfact($n){
$factorial=$n;
while (--
$n>1) $factorial=bcmul($factorial,$n);
return
$factorial;
}

print
bcfact(50);
//30414093201713378043612608166064768844377641568960512000000000000
?>
-11
Artur Kuritsyn
6 年前
对 bcround 有用的函数

function getBcRound($number, $precision = 0)
{
$precision = ($precision < 0)
? 0
: (int) $precision;
if (strcmp(bcadd($number, '0', $precision), bcadd($number, '0', $precision+1)) == 0) {
return bcadd($number, '0', $precision);
}
if (getBcPresion($number) - $precision > 1) {
$number = getBcRound($number, $precision + 1);
}
$t = '0.' . str_repeat('0', $precision) . '5';
return $number < 0
? bcsub($number, $t, $precision)
: bcadd($number, $t, $precision);
}

function getBcPresion($number) {
$dotPosition = strpos($number, '.');
if ($dotPosition === false) {
return 0;
}
return strlen($number) - strpos($number, '.') - 1;
}

var_dump(getBcRound('3', 0) == number_format('3', 0));
var_dump(getBcRound('3.4', 0) == number_format('3.4', 0));
var_dump(getBcRound('3.56', 0) == number_format('3.6', 0));
var_dump(getBcRound('1.95583', 2) == number_format('1.95583', 2));
var_dump(getBcRound('5.045', 2) == number_format('5.045', 2));
var_dump(getBcRound('5.055', 2) == number_format('5.055', 2));
var_dump(getBcRound('9.999', 2) == number_format('9.999', 2));
var_dump(getBcRound('5.0445', 5) == number_format('5.044500', 5));
var_dump(getBcRound('5.0445', 4) == number_format('5.04450', 4));
var_dump(getBcRound('5.0445', 3) == number_format('5.0445', 3));
var_dump(getBcRound('5.0445', 2) == number_format('5.045', 2));
var_dump(getBcRound('5.0445', 1) == number_format('5.05', 1));
var_dump(getBcRound('5.0445', 0) == number_format('5.0', 0));//
var_dump(getBcRound('5.04455', 2) == number_format('5.045', 2));
var_dump(getBcRound('99.999', 2) == number_format('100.000', 2));
var_dump(getBcRound('99.999') == number_format('99.999', 0));
var_dump(getBcRound('99.999', 'a') == number_format('99.999', 0));
var_dump(getBcRound('99.999', -1.5) == number_format('99.999', 0));
var_dump(getBcRound('-0.00001', 2) == number_format('-0.000', 2));
var_dump(getBcRound('-0.0000', 2) == number_format('0', 2));
var_dump(getBcRound('-4.44455', 2) == number_format('-4.445', 2));
var_dump(getBcRound('-4.44555', 0) == number_format('-4.5', 0));
var_dump(getBcRound('-4.444444444444444444444444444444444444444444445', 0) == number_format('-4.5', 0));
-24
George Lund
10 年前
值得注意的是,这个库的名字非常错误。

它可能被称为“二进制计算器”,但您得到的是一个可以准确表示基数为 10 的分数的十进制计算器。
-19
匿名
6 年前
对 bcround 有用的函数

function getBcRound($number, $precision = 0)
{
$precision = ($precision < 0)
? 0
: (int) $precision;
if (strcmp(bcadd($number, '0', $precision), bcadd($number, '0', $precision+1)) == 0) {
return bcadd($number, '0', $precision);
}
if (getBcPresion($number) - $precision > 1) {
$number = getBcRound($number, $precision + 1);
}
$t = '0.' . str_repeat('0', $precision) . '5';
return $number < 0
? bcsub($number, $t, $precision)
: bcadd($number, $t, $precision);
}

function getBcPresion($number) {
$dotPosition = strpos($number, '.');
if ($dotPosition === false) {
return 0;
}
return strlen($number) - strpos($number, '.') - 1;
}

var_dump(getBcRound('3', 0) == number_format('3', 0));
var_dump(getBcRound('3.4', 0) == number_format('3.4', 0));
var_dump(getBcRound('3.56', 0) == number_format('3.6', 0));
var_dump(getBcRound('1.95583', 2) == number_format('1.95583', 2));
var_dump(getBcRound('5.045', 2) == number_format('5.045', 2));
var_dump(getBcRound('5.055', 2) == number_format('5.055', 2));
var_dump(getBcRound('9.999', 2) == number_format('9.999', 2));
var_dump(getBcRound('5.0445', 5) == number_format('5.044500', 5));
var_dump(getBcRound('5.0445', 4) == number_format('5.04450', 4));
var_dump(getBcRound('5.0445', 3) == number_format('5.0445', 3));
var_dump(getBcRound('5.0445', 2) == number_format('5.045', 2));
var_dump(getBcRound('5.0445', 1) == number_format('5.05', 1));
var_dump(getBcRound('5.0445', 0) == number_format('5.0', 0));//
var_dump(getBcRound('5.04455', 2) == number_format('5.045', 2));
var_dump(getBcRound('99.999', 2) == number_format('100.000', 2));
var_dump(getBcRound('99.999') == number_format('99.999', 0));
var_dump(getBcRound('99.999', 'a') == number_format('99.999', 0));
var_dump(getBcRound('99.999', -1.5) == number_format('99.999', 0));
var_dump(getBcRound('-0.00001', 2) == number_format('-0.000', 2));
var_dump(getBcRound('-0.0000', 2) == number_format('0', 2));
var_dump(getBcRound('-4.44455', 2) == number_format('-4.445', 2));
var_dump(getBcRound('-4.44555', 0) == number_format('-4.5', 0));
var_dump(getBcRound('-4.444444444444444444444444444444444444444444445', 0) == number_format('-4.5', 0));
To Top