- BC 数学 函数
- bcadd — 2个任意精度数字的加法计算
- bccomp — 比较两个任意精度的数字
- bcdiv — 2个任意精度的数字除法计算
- bcmod — 对一个任意精度数字取模
- bcmul — 2个任意精度数字乘法计算
- bcpow — 任意精度数字的成方
- bcpowmod — Raise an arbitrary precision number to another, reduced by a specified modulus
- bcscale — 设置所有bc数学函数的默认小数点保留位数
- bcsqrt — 任意精度数字的二次方根
- bcsub — 2个任意精度数字的减法
bcadd
string bcadd ( string $left_operand
, string $right_operand
[, int $scale
] )
左操作数
和右操作数
求和
<?php
$a = '1.234';
$b = '5';
echo bcadd($a, $b); // 6
echo bcadd($a, $b, 4); // 6.2340
?>
bccomp
int bccomp ( string $left_operand
, string $right_operand
[, int $scale
= int ] )
把right_operand
和left_operand
作比较, 并且返回一个整数的结果.
返回值
如果两个数相等返回0, 左边的数left_operand
比较右边的数right_operand
大返回1, 否则返回-1.
<?php
echo bccomp('1', '2') . "\n"; // -1
echo bccomp('1.00001', '1', 3); // 0
echo bccomp('1.00001', '1', 5); // 1
?>
bcdiv
string bcdiv ( string $left_operand
, string $right_operand
[, int $scale
= int ] )
左操作数
除以右操作数
返回值
返回结果为字符串类型的结果,如果右操作数
是0结果为null
<?php
echo bcdiv('105', '6.55957', 3); // 16.007
?>
bcmod
string bcmod ( string $left_operand
, string $modulus
)
对左操作数使用系数
取模
返回值
返回字符串类型取模后结果,如果系数为0则返回null
<?php
echo bcmod('4', '2'); // 0
echo bcmod('2', '4'); // 2
?>
bcmul
string bcmul ( string $left_operand
, string $right_operand
[, int $scale
= int ] )
左操作数
乘以右操作数
返回值
返回结果为字符串类型.
<?php
echo bcmul('1.34747474747', '35', 3); // 47.161
echo bcmul('2', '4'); // 8
?>
bcpow
string bcpow ( string $left_operand
, string $right_operand
[, int $scale
] )
左操作数
的右操作数
次方运算.
返回值
返回结果为字符串类型.
<?php
echo bcpow('4.2', '3', 2); // 74.08
?>
bcpowmod
将任意精度数提高到另一个精度,并减少指定的模数
string bcpowmod ( string $left_operand
, string $right_operand
, string $modulus
[, int $scale
= 0 ] )
使用快速求幂法将左_操作数相对于模,提升为右_幂操作数。
返回值
以字符串形式返回结果,如果模数为0,则返回空值。
<?php
$a = bcpowmod($x, $y, $mod);
$b = bcmod(bcpow($x, $y), $mod);
//$a和$b是相等的
?>
bcscale
bool bcscale ( int $scale
)
设置所有bc数学函数的未设定情况下得小数点保留位数.
返回值
成功时返回 TRUE
, 或者在失败时返回 FALSE
。
<?php
// default scale : 3
bcscale(3);
echo bcdiv('105', '6.55957'); // 16.007
// this is the same without bcscale()
echo bcdiv('105', '6.55957', 3); // 16.007
?>
bcsqrt
说明
string bcsqrt ( string $operand
[, int $scale
] )
返回操作数
的二次方根.
返回值
返回二次方根的结果为字符串类型,如果操作数是负数则返回null.
<?php
echo bcsqrt('2', 3); // 1.414
?>
bcsub
string bcsub ( string $left_operand
, string $right_operand
[, int $scale
= int ] )
左操作数
减去右操作数
.
返回值
返回减法之后结果为字符串类型.
<?php
$a = '1.234';
$b = '5';
echo bcsub($a, $b); // -3
echo bcsub($a, $b, 4); // -3.7660
?>