PHPackages                             ibelousov/math-exec - 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. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. ibelousov/math-exec

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

ibelousov/math-exec
===================

Evaluate expressions from strings using variable precision numbers

2.0.0(6y ago)17MITPHPPHP &gt;=7.2CI failing

Since Apr 4Pushed 6y ago1 watchersCompare

[ Source](https://github.com/ibelousov/math-exec)[ Packagist](https://packagist.org/packages/ibelousov/math-exec)[ RSS](/packages/ibelousov-math-exec/feed)WikiDiscussions master Synced 6d ago

READMEChangelog (3)Dependencies (1)Versions (4)Used By (0)

MathExec
========

[](#mathexec)

MathExec is a php library for parsing and evaluating math expressions like this:

```
echo Evaluator::mathExec("5 ^ 2 + 36 / 2 - 2 * 0.5");
```

Or you could prepare an expression template and then execute it, with specified precision, when needed, by providing needed variables

```
$evaluator = Evaluator::mathPrepare("a + 5 / 3 * b - c");

foreach([[1,2,3,4],[4,5,6,4],[7,8,9,0]] as $values) {
    $evaluator->exec(['a' => $values[0], 'b' => $values[1], 'c' => $values[2]], $values[3]);
}
```

or like this:

```
$a = 1.5E+2;
$b = 18;

return Evaluator::mathExec("$a * 4 + 30 * 2 + $b / 3");
```

and this also is true:

```
var_dump(Evaluator::mathExec("floor((0.7+0.1)*10)") == 8);
// boolean true

var_dump([floor((0.7+0.1)*10) == 8]);
// boolean false
```

It utilizes a Pratt parser, and uses ext-bcmath extension to evaluate operations. For example this expression

```
$a = 4;
$b = 4;
$c = 4;

Evaluator::mathExec("$a + $b / $c", 40);
```

internally turns into this

```
bcadd($a, bcdiv($b, $c, 40), 40);
```

it is very nice and convenient to write expressions naturally, without using bcadd, bcdiv, etc.

Installation
------------

[](#installation)

Use the package manager composer to install it

```
composer install ibelousov/math-exec
```

Usage
-----

[](#usage)

First of all import class Evaluator:

```
use \Ibelousov\MathExec\Evaluator\Evaluator;
```

### Supported things

[](#supported-things)

```
// Root
Evaluator::mathExec("\\2");
// "1.1892071150027210667174999705604759152929"
```

(you could set inner precision, like this:

```
Evaluator::mathExec("\\2", 1000);

// "1.1892071150027210667174999705604759152929720924638174130190022
// 2471946666822691715987078134453813767371603739477476921318606372
// 6361789847756785360862538017775070151511403557092273162342868889
// 9241754460719087105038499725591050098371044920154845735674580904
// 8399409309000349779590803848965884300504119871700937907982098462
// 5235373981281740818113780828552014842210060958932412445931035057
// 5191963029413832634742802798244080228008217292720586153666393704
// 0023820730854565306744771485988873345762718678381165470458727612
// 7111269988678434930175861424970170054131455143891998743766762178
// 5161783177987307048236318734734842180537156986842636482761056228
// 4779958628963329392816878747586560347379199645940075615444371574
// 1890303986971294306248625351734129153597531121544674615908647760
// 6517445957055930979119465756398917686972170262497475333629918606
// 5311570834936807698049481706074376847467855865282550141846497924
// 8909951563378299859508764353239662147789654791045418693466186139
// 614521856391702634160435422985610854932687"
```

by default it is 40 signs after point )

// Multiplication

```
Evaluator::mathExec("2 * 2", 0);
//  "4"
```

// Division

```
Evaluator::mathExec("2 / 2", 0);
// "1"
```

```
// Power
Evaluator::mathExec("2 ^ 3", 0);
// "8" (left and right should be whole numbers)
```

```
// Modul
Evaluator::mathExec("7 % 2", 0);
// "1"
```

```
// Whole division
Evaluator::mathExec("3.1415 // 2", 0);
// "1"
```

```
// Associativity
Evaluator::mathExec("2 + 2 * 2", 0);
// "6"
```

```
// Parenthesis
Evaluator::mathExec("(2 + 2) * 2", 0);
// "8"
```

```
// Float to string convertion number formats
$a = 0.1415E-10;
$b = 0.1415E-10;
Evaluator::mathExec("$a * $b");
// "0.0000000000000000000002002225000000000000"
```

```
// comparison
Evaluator::mathExec("4 ^ 128 > 4 ^ 64");
// "1"
```

```
Evaluator::mathExec("4 ^ 128 < 4 ^ 64");
// "0"
```

```
Evaluator::mathExec("4 ^ 64 + 1 >= 4 ^ 64");
// "1"
```

```
Evaluator::mathExec("4 ^ 64
