PHPackages                             sixteenstudio/mathematician - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. sixteenstudio/mathematician

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

sixteenstudio/mathematician
===========================

A layer on top of BCMath that deals with fractional calculations

0.0.5(8y ago)0201[1 issues](https://github.com/sixteenstudio/mathematician/issues)PHP

Since Jan 31Pushed 8y ago2 watchersCompare

[ Source](https://github.com/sixteenstudio/mathematician)[ Packagist](https://packagist.org/packages/sixteenstudio/mathematician)[ RSS](/packages/sixteenstudio-mathematician/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (5)DependenciesVersions (7)Used By (0)

Mathematician
=============

[](#mathematician)

A layer on top of BCMath that deals with fractional calculations

Requirements
------------

[](#requirements)

The BCMath PHP extension needs to be required in order for this to work.

This package has been developed for PHP 7.0^

Usage
-----

[](#usage)

Instead of using standard BCMath functions such as `bcadd` and `bcsub`, use the `math_` functions provided within the package.

`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`

These functions return an instance of `Sixteenstudio\Mathematician\Number`, unlike BCMath which returns a string representation of the decimal result of the calculation.

This allows us to do some neat things like represent certain operations as fractions, which in turn allows us to do things like this far more accurately than the likes of standard float logic or BCMath can handle:

`(1 / 3) * 3 // Equates to 0.9999999999813`

`bcmul(bcdiv(1, 3, 20), 3, 20) // Equates to 0.99999999999999999997`

`math_mul(math_div(1, 3, 20), 3, 20) // Equates to 1.00000000000000000000`

If you'd prefer not to use the helpers, you can insantiate the `Sixteenstudio\Mathematician\Mathematician` class and use the respective functions directly like so:

```
