BCMath高精度数学类

各参数可以是字符串,也可以是原值。
由于精度问题,建议参数都以字符串形式传递。
属性
命名空间 fize\math
类名 Bc
方法:
方法名 说明
add() 返回2个任意精度数字的加法计算,返回字符串结果
adds() 累加多个任意精度数字的加法计算
comp() 把right_operand和left_operand作比较, 并且返回一个整数的结果.
div() 2个任意精度的数字除法计算
mod() 对一个任意精度数字取模
mul() 2个任意精度数字乘法计算
muls() 多个任意精度数字的乘法法计算,返回字符串结果
pow() 任意精度数字的乘方
powmod() 对乘方结果进行取模
scale() 设置所有bc数学函数的默认小数点保留位数
sqrt() 任意精度数字的二次方根
sub() 2个任意精度数字的减法

方法

add()

返回2个任意精度数字的加法计算,返回字符串结果

public static function add (
    mixed $left_operand,
    mixed $right_operand,
    int $scale = null
) : string
参数:
名称 说明
left_operand 第一个数字
right_operand 第二个数字
scale 指定结果小数位,默认是自动

adds()

累加多个任意精度数字的加法计算

public static function adds (
    array $operands,
    int $scale = null
) : string
参数:
名称 说明
operands 要累加的数值
scale 指定结果小数位,默认是自动

comp()

把right_operand和left_operand作比较, 并且返回一个整数的结果.

public static function comp (
    mixed $left_operand,
    mixed $right_operand,
    int $scale = null
) : int
参数:
名称 说明
left_operand 左边的运算数
right_operand 右边的运算数
scale 设置指示数字, 在使用来作比较的小数点部分. 默认比较全部
返回值:

如果两个数相等返回0, 左边的数left_operand比较右边的数right_operand大返回1, 否则返回-1.

div()

2个任意精度的数字除法计算

public static function div (
    mixed $left_operand,
    mixed $right_operand,
    int $scale = null
) : string
参数:
名称 说明
left_operand 被除数
right_operand 除数
scale 指定结果小数位,默认是自动
返回值:

返回结果为字符串类型的结果,如果右操作数是0结果为null

mod()

对一个任意精度数字取模

public static function mod (
    mixed $left_operand,
    mixed $modulus
) : string
参数:
名称 说明
left_operand 左操作数
modulus 系数
返回值:

返回字符串类型取模后结果,如果系数为0则返回null

mul()

2个任意精度数字乘法计算

public static function mul (
    mixed $left_operand,
    mixed $right_operand,
    int $scale = null
) : string
参数:
名称 说明
left_operand 左操作数
right_operand 右操作数
scale 设置结果中小数点后的小数位数
返回值:

返回计算结果字符串

muls()

多个任意精度数字的乘法法计算,返回字符串结果

public static function muls (
    array $operands,
    int $scale = null
) : string
参数:
名称 说明
operands 要累乘的数值组成的驻足
scale 指定结果小数位,默认是自动

pow()

任意精度数字的乘方

public static function pow (
    mixed $left_operand,
    mixed $right_operand,
    int $scale = null
) : string
参数:
名称 说明
left_operand 底数
right_operand 乘方
scale 设置结果中小数点后的小数位数

powmod()

对乘方结果进行取模

public static function powmod (
    mixed $left_operand,
    mixed $right_operand,
    mixed $modulus,
    int $scale = null
) : string
参数:
名称 说明
left_operand 底数
right_operand 乘方
modulus
scale 设置结果中小数点后的小数位数

scale()

设置所有bc数学函数的默认小数点保留位数

public static function scale (
    int $scale
) : bool
参数:
名称 说明
scale 小数点保留位数.
返回值:

成功时返回 TRUE , 或者在失败时返回 FALSE

sqrt()

任意精度数字的二次方根

public static function sqrt (
    mixed $operand,
    int $scale = null
) : string
参数:
名称 说明
operand 操作数
scale 设置结果中小数点后的小数位数

sub()

2个任意精度数字的减法

public static function sub (
    mixed $left_operand,
    mixed $right_operand,
    int $scale = null
) : string
参数:
名称 说明
left_operand 被减数
right_operand 减数
scale 设置结果中小数点后的小数位数