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

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

avadim/ace-calculator
=====================

Flexible universal calculator with custom operators, functions and variables

v3.1.2(1y ago)021.3k↓17.6%MITPHPPHP &gt;=7.4CI failing

Since Feb 10Pushed 1y ago1 watchersCompare

[ Source](https://github.com/aVadim483/ace-calculator)[ Packagist](https://packagist.org/packages/avadim/ace-calculator)[ Docs](https://github.com/aVadim483/ace-calculator)[ RSS](/packages/avadim-ace-calculator/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (10)Dependencies (1)Versions (19)Used By (0)

AceCalculator - flexible universal calculator in PHP
====================================================

[](#acecalculator---flexible-universal-calculator-in-php)

You can calculate classical mathematical expressions with variables, or you can specify your own calculation rules, operators or custom functions

Forked from NeonXP/MathExecutor (), but advanced and improved.

Jump To:

- [Installation](#installation)
- [Simple Usage](#simple-usage)
- [Default operators and functions](#default-operators-and-functions)
- [Variables](#variables)
- [Multiple expressions](#multiple-expressions)
- [Extra operators and functions](#extra-operators-and-functions)
- [Custom functions](#custom-functions)
- [Custom operators](#custom-operators)
- [Interpreting of identifiers](#interpreting-of-identifiers)
- [Non-numeric values](#non-numeric-values)
- [Error Handlers](#error-handlers)

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

[](#installation)

|$ composer require avadim/ace-claculator

All instructions to install here:

Sample Usage
------------

[](#sample-usage)

```
require 'vendor/autoload.php';
// create the calculator
$calculator = new \avadim\AceClaculator\AceClaculator();

// calculate expression
print $calculator->execute('1 + 2 * (2 - (4+10))^2 + sin(10)');

// cascade execution - you can calculate a series of expressions
// variable $_ has result of previous calculation
print $calculator
        ->calc('4+10')
        ->calc('1 + 2 * (2 - $_)^2') // the variable $_ contains the result of the last calculation
        ->calc('$_ + sin(10)')
        ->result();
```

Default operators, functions and constants
------------------------------------------

[](#default-operators-functions-and-constants)

Default operators: `+ - * / ^`

Arithmetic functions

- abs()
- avg()
- ceil()
- exp()
- expm1()
- floor()
- fmod()
- hypot()
- intdiv()
- log()
- log10()
- log1p()
- max()
- min()
- sqrt()
- round()

Trigonometric functions

- acos()
- acosh()
- asin()
- asinh()
- atan()
- atan2()
- atanh()
- atn() (alias of atan)
- cos()
- cosh()
- deg2rad()
- degrees() (alias of rad2deg)
- rad2deg()
- radians() (alias of deg2rad)
- sin()
- sinh()
- tan()
- tanh()
- tn() (alias of tan)

Default constants

PI = 3.14159265358979323846 E = 2.7182818284590452354

Also you can use any standard math constants from PHP - M\_LOG2E, M\_PI\_2 etc

```
$calculator->execute('cos(PI)');
$calculator->execute('cos(M_PI)'); // the same result
```

Variables
---------

[](#variables)

You can add own variables to executor and use their in expressions

```
$calculator->setVars([
    'var1' => 0.15,
    'var2' => 0.22
]);

// calculation with variables
$calculator->execute('$var1 + $var2');

// calculate and assign result to $var3
$calculator->execute('$var1 + $var2', '$var3');

// assign values to variable in expression
$calculator
    ->calc('$var3 = ($var1 + $var2)')
    ->calc('$var3 * 20')
    ->result();
```

Multiple expressions
--------------------

[](#multiple-expressions)

You can execute multiple expressions in one by separating them with a semicolon

```
$result1 = $calculator
    ->setVar('$var1', 0.15)
    ->setVar('$var2', 0.22)
    ->calc('$var3 = $var1 + $var2')
    ->calc('$var3 * 20')
    ->result()
;
// $result2 will be equal $result1
$result2 = $calculator->execute('$var1=0.15; $var2=0.22; $var3 = $var1 + $var2; $var3 * 20');
```

Extra operators and functions
-----------------------------

[](#extra-operators-and-functions)

You can load extensions with extra operators and functions by method `loadExtension()`:

```
// load extension 'Bool'
$calculator->loadExtension('Bool');
```

This extension load boolean operators: `<  >= == != && ||`

You can use boolean operators with extra function `if()`

```
print $calculator->execute('if(100+20+3 > 111, 23, 34)');
```

Custom functions
----------------

[](#custom-functions)

Add custom function to executor:

```
$calculator->addFunction('dummy', function($a) {
    // do something
    return $result;
});

print $calculator->execute('dummy(123)');

// If the function takes more than 1 argument, you must specify this

// New function hypotenuse() with 2 arguments
$calculator->addFunction('hypotenuse', function($a, $b) {
    return sqrt($a^2 + $b^2);
}, 2);

// New function nround()
//   1 - minimum number of arguments
//   true - used optional arguments
$calculator->addFunction('nround', function($a, $b = 0) {
    return round($a,  $b);
}, 1, true);

print $calculator->execute('nround(hypotenuse(3,4), 2)');
```

Custom operators
----------------

[](#custom-operators)

A simple way to add an operator

```
use avadim\AceCalculator\Token\Operator\TokenOperator;
$func = function (array &$stack)
{
    $op2 = array_pop($stack);
    $op1 = array_pop($stack);

    return $op1->getValue() % $op2->getValue();
};

$calculator->addOperator('mod', [TokenOperator::MATH_PRIORITY_DIVIDE, $func]);
echo $calculator->execute('286 mod 100');
```

Alternative way to add operator using specified class. Create the class of custom operator

```
