PHP bcmath as a static class, with several enhancements.
Use Composer:
composer require danhunsaker/bcmath
Usage is nearly identical to the bcmath extension functions. The main
difference is that the bc
prefix is replaced by the Danhunsaker\BC
class
name (which you can easily alias in your project(s) via use Danhunsaker\BC
).
In other words:
bcadd()
becomesBC::add()
bccomp()
becomesBC::comp()
bcdiv()
becomesBC::div()
bcmod()
becomesBC::mod()
bcmul()
becomesBC::mul()
bcpow()
becomesBC::pow()
bcpowmod()
becomesBC::powmod()
bcscale()
becomesBC::scale()
bcsqrt()
becomesBC::sqrt()
bcsub()
becomesBC::sub()
There are also some additional convenience methods available, that aren't present in the extension:
BC::epow()
raises e to the argument's power.BC::fact()
calculates the factorial of the argument.BC::ln()
gives the natural logarithm of the argument.BC::log()
gives the base 10 logarithm of the argument (uses ln $val/ln 10).BC::max()
returns the largest value in an array (the first argument).BC::min()
returns the smallest value in an array (the first argument).BC::modfrac()
behaves exactly likeBC::mod()
, except it will return the fractional part of any remainder as well as the integer part.BC::powfrac()
supports fractional exponents, allowing roots other than the square to be calculated.BC::root()
is a complement toBC::powfrac()
, and is in fact just a convenience wrapper for it.BC::round()
rounds a value to a given scale.
Contributions are welcome at any time on GitHub.
Security issues should be reported directly to Dan Hunsaker via email.