PHPackages                             chippyash/math-type-calculator - 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. chippyash/math-type-calculator

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

chippyash/math-type-calculator
==============================

Calculators for the chippyash/strong-type numeric types

3.1.0(7y ago)128721BSD-3-ClausePHPPHP &gt;=5.6CI failing

Since Aug 4Pushed 7y ago1 watchersCompare

[ Source](https://github.com/chippyash/Math-Type-Calculator)[ Packagist](https://packagist.org/packages/chippyash/math-type-calculator)[ Docs](http://zf4.biz/packages?utm_source=packagist&utm_medium=web&utm_campaign=blinks&utm_content=typecalculator)[ RSS](/packages/chippyash-math-type-calculator/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependencies (2)Versions (11)Used By (1)

chippyash/math-type-calculator
==============================

[](#chippyashmath-type-calculator)

Quality Assurance
-----------------

[](#quality-assurance)

[![PHP 5.4](https://camo.githubusercontent.com/27d753c231a6c9cfd43aa1ecc519bca8365f6a01ce6004452d4b3ca86e5b6756/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d352e342d626c75652e737667)](https://camo.githubusercontent.com/27d753c231a6c9cfd43aa1ecc519bca8365f6a01ce6004452d4b3ca86e5b6756/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d352e342d626c75652e737667)[![PHP 5.5](https://camo.githubusercontent.com/219e00452ba9998fcbfe00213b6eb56a8b86d19188783ed34d770d27b041df67/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d352e352d626c75652e737667)](https://camo.githubusercontent.com/219e00452ba9998fcbfe00213b6eb56a8b86d19188783ed34d770d27b041df67/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d352e352d626c75652e737667)[![PHP 5.6](https://camo.githubusercontent.com/88093c79af42bd3c07f4d6aa378289e1f5450411c56753b0323bd7d8b9b1f9ee/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d352e362d626c75652e737667)](https://camo.githubusercontent.com/88093c79af42bd3c07f4d6aa378289e1f5450411c56753b0323bd7d8b9b1f9ee/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d352e362d626c75652e737667)[![PHP 7](https://camo.githubusercontent.com/d23ce60b89c28c023d0ca69981ec9afbb17eb08a9cd1b609fd84c15d0732b7ce/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d372d626c75652e737667)](https://camo.githubusercontent.com/d23ce60b89c28c023d0ca69981ec9afbb17eb08a9cd1b609fd84c15d0732b7ce/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d372d626c75652e737667)[![Build Status](https://camo.githubusercontent.com/3ae4e4a79085bf8de41a756a052f94636a5ecc33d23493a69928a76e5ffacc82/68747470733a2f2f7472617669732d63692e6f72672f6368697070796173682f4d6174682d547970652d43616c63756c61746f722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/chippyash/Math-Type-Calculator)[![Test Coverage](https://camo.githubusercontent.com/60b82da93fef526770893cdac4b1cd39bac8b4d11aecfa3d6d6637d078a142a0/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f6368697070796173682f4d6174682d547970652d43616c63756c61746f722f6261646765732f636f7665726167652e737667)](https://codeclimate.com/github/chippyash/Math-Type-Calculator/coverage)[![Code Climate](https://camo.githubusercontent.com/b71069de5ea6aa063d94b92449cce6383c682e211c4011405acc0db218f4e0d7/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f6368697070796173682f4d6174682d547970652d43616c63756c61746f722f6261646765732f6770612e737667)](https://codeclimate.com/github/chippyash/Math-Type-Calculator)

See the [test contract](https://github.com/chippyash/Math-Type-Calculator/blob/master/docs/Test-Contract.md).

The above badges represent the current master branch. As a rule, I don't push to GitHub unless tests, coverage and usability are acceptable. This may not be true for short periods of time; on holiday, need code for some other downstream project etc. If you need stable code, use a tagged version. Read 'Further Documentation' and 'Installation'.

Current development branch is feature/gmp\_support. Want to help? - that's where it is.

Please note that developer support for PHP5.4 &amp; 5.5 was withdrawn at version 3.0.0 of this library. If you need support for PHP 5.4 or 5.5, please use a version `>=2,add($r, $w) . PHP_EOL;
    echo $calc->add($c1, $c2) . PHP_EOL;
    echo $calc->add($i, $f) . PHP_EOL;
    echo $calc->sub($c1, $c2) . PHP_EOL;
    echo $calc->sub($n, $w) . PHP_EOL;
    echo $calc->add($r, $w) . PHP_EOL;
    echo $calc->add($r, $w) . PHP_EOL;
    echo $calc->add($r, $w) . PHP_EOL;
```

The Calculator supports the following methods (all operands are NumericTypeInterface, or PHP int or PHP float):

- add($a, $b) : NumericTypeInterface
- sub($a, $b) : NumericTypeInterface
- mul($a, $b : NumericTypeInterface
- div($a, $b) : NumericTypeInterface
- reciprocal($a) : NumericTypeInterface
- pow($base, $exp) : NumericTypeInterface
- sqrt($a) : NumericTypeInterface

The Calculator will arbitrate between types and return the lowest possible type based on the operand types. The order of precedence is

- ComplexType
- RationalType
- FloatType
- IntType (including WholeIntType and NaturalIntType)

Be careful with complex types, they can only be converted down if they are real, i.e. the imaginary part == 0

The sqrt() method is provided as a convenience, you can use pow(n, 1/e) e.g. pow(4, 1/2) == sqrt(4)

For a demonstration of all the available operations between types and their resultant types run the examples/example-calc.php file

#### Comparisons

[](#comparisons)

To compare two numeric types:

```
    use Chippyash\Math\Type\Comparator;
    use Chippyash\Type\TypeFactory;

    $r = TypeFactory::create('rational', 2, 3);
    $i = TypeFactory::create('int', 23);
    $w = TypeFactory::create('whole', 3);
    $n = TypeFactory::create('natural', 56);
    $f = TypeFactory::create('float', 19.6);
    $c1 = TypeFactory::create('complex', '2+3i');
    $c2 = TypeFactory::create('complex', '-6+4i');

    $comp = new Comparator();

    if ($comp->compare($r, $i) == 0) {...}
    if ($comp->compare($c1, $c2) == -1) {...}
    if ($comp->compare($w, $n) == 1) {...}
```

The Comparator::compare($a, $b) method takes two NumericTypeInterface types and returns

```
a == b: 0
a < b : -1
a > b : 1

```

It has convenience methods (all operands are NumericTypeInterface):

- eq($a, $b) : boolean: $a == $b
- neq($a, $b) : boolean: $a != $b
- lt($a, $b) : boolean: $a &lt; $b
- lte($a, $b) : boolean: $a &lt;= $b
- gt($a, $b) : boolean: $a &gt; $b
- gte($a, $b) : boolean: $a &gt;= $b

```
    if ($comp->gt($w, $f) { ... }
```

### Changing the library

[](#changing-the-library)

1. fork it
2. write the test
3. amend it
4. do a pull request

Found a bug you can't figure out?

1. fork it
2. write the test
3. do a pull request

NB. Make sure you rebase to HEAD before your pull request

Where?
------

[](#where)

The library is hosted at [Github](https://github.com/chippyash/Math-Type-Calculator). It is available at [Packagist.org](https://packagist.org/packages/chippyash/math-type-calculator)

### Installation

[](#installation)

Install [Composer](https://getcomposer.org/)

#### For production

[](#for-production)

add

```
    "chippyash/math-type-calculator": ">=3,
