十六进制工作室 / 数学家
一个在BCMath之上处理分数计算的层
This package is auto-updated.
Last update: 2024-08-28 02:06:12 UTC
README
一个在BCMath之上处理分数计算的层
需求
要使此功能正常工作,需要要求BCMath PHP扩展。
此软件包是为PHP 7.0开发。
使用方法
而不是使用标准BCMath函数,如bcadd
和bcsub
,请使用软件包内提供的math_
函数。
math_add($leftOperand, $rightOperand, $scale): Sixteenstudio\Mathematician\Number
math_sub($leftOperand, $rightOperand, $scale): Sixteenstudio\Mathematician\Number
math_mul($leftOperand, $rightOperand, $scale): Sixteenstudio\Mathematician\Number
math_div($leftOperand, $rightOperand, $scale): Sixteenstudio\Mathematician\Number
这些函数返回一个Sixteenstudio\Mathematician\Number
实例,与BCMath返回计算结果的字符串表示不同。
这使得我们可以做一些很酷的事情,比如将某些操作表示为分数,从而可以比标准浮点逻辑或BCMath更精确地执行此类操作
(1 / 3) * 3 // 等于 0.9999999999813
bcmul(bcdiv(1, 3, 20), 3, 20) // 等于 0.99999999999999999997
math_mul(math_div(1, 3, 20), 3, 20) // 等于 1.00000000000000000000
如果您不想使用助手,可以直接实例化Sixteenstudio\Mathematician\Mathematician
类并使用相应的函数,如下所示
<?php $mathematician = new \Sixteenstudio\Mathematician\Mathematician(); $addition = $mathematician->add(1, 5, 20); $subtraction = $mathematician->subtract(1, 5, 20); $multiplication = $mathematician->multiply(1, 5, 20); $division = $mathematician->divide(1, 5, 20);