2017 © Pedro Peláez
 

library math-executor

Math expressions calculator with custom operators, functions and variables

image

avadim/math-executor

Math expressions calculator with custom operators, functions and variables

  • Tuesday, January 9, 2018
  • by avadim
  • Repository
  • 1 Watchers
  • 0 Stars
  • 26 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 13 Forks
  • 0 Open issues
  • 11 Versions
  • 0 % Grown

The README.md

MathExecutor

License Latest Stable Version, (*1)

Math expressions calculator with custom operators, functions and variables, (*2)

This package no longer supported! Please use AceCalculator instead! https://github.com/aVadim483/ace-calculator, (*3)

Install via Composer

|$ composer require avadim/math-executor, (*4)

All instructions to install here: https://packagist.org/packages/avadim/math-executor, (*5)

Sample usage

require 'vendor/autoload.php';

$calculator = new \avadim\MathExecutor\MathExecutor();

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

// cascade execution - variable $_ has result of previous calculation
print $calculator
        ->calc('4+10')
        ->calc('1 + 2 * (2 - $_)^2')
        ->calc('$_ + sin(10)')
        ->getResult();

Default operators and functions

Default operators: + - * / ^, (*6)

Default functions: * sin * cos * tn * asin * acos * atn * min * max * avg, (*7)

Variables

Default variables:, (*8)

$pi = 3.14159265359
$e = 2.71828182846

You can add own variable to executor:, (*9)

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

$calculator->execute('$var1 + $var2');

Extra operators and functions

To load extra operators and functions use method loadExtra():, (*10)

$calculator->loadExtra();

Extra operators are boolean operators: < <= > >= == != You can use boolean operators with function if(), (*11)

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

Custom functions

Add custom function to executor:, (*12)

$calculator->addFunction('hypotenuse', function($a, $b) {
    return sqrt($a ** 2 + $b ** 2);
}, 2);

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

Custom operators

Add custom operator to executor:, (*13)

<?php
use avadim\MathExecutor\Generic\AbstractToken;
use avadim\MathExecutor\Generic\AbstractTokenOperator;
use avadim\MathExecutor\Token\TokenScalarNumber;

class TokenOperatorModulus extends AbstractTokenOperator
{
    protected static $pattern = 'mod';

    /**
     * Priority of this operator (1 equals "+" or "-", 2 equals "*" or "/", 3 equals "^")
     * @return int
     */
    public function getPriority()
    {
        return 3;
    }

    /**
     * Association of this operator (self::LEFT_ASSOC or self::RIGHT_ASSOC)
     * @return string
     */
    public function getAssociation()
    {
        return self::LEFT_ASSOC;
    }

    /**
     * Execution of this operator
     * @param AbstractToken[] $stack Stack of tokens
     *
     * @return TokenScalarNumber
     */
    public function execute(&$stack)
    {
        $op2 = array_pop($stack);
        $op1 = array_pop($stack);
        $result = $op1->getValue() % $op2->getValue();

        return new TokenScalarNumber($result);
    }
}

And adding to executor:, (*14)

$calculator = new avadim\MathExecutor\MathExecutor();
$calculator->addOperator('mod', '\TokenOperatorModulus');
echo $calculator->execute('286 mod 100');

Interpreting of identifiers

Identifiers - start with a letter and consist of a sequence of letters and numbers. You can specify rules how to interpret them in calculations, (*15)

$calculator->setIdentifiers([
    'ONE' => 1,
    'YEAR' => function($variables, $identifiers) { return date('Y'); },
]);

$calculator->execute('YEAR + ONE');

The Versions

09/01 2018

dev-master

9999999-dev https://github.com/aVadim483/MathExecutor

Math expressions calculator with custom operators, functions and variables

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Vadim Shemarov aka aVadim

parser math expression calculator

08/01 2018

v1.2.1

1.2.1.0 https://github.com/aVadim483/MathExecutor

Math expressions calculator with custom operators, functions and variables

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Vadim Shemarov aka aVadim

parser math expression calculator

08/01 2018

v1.2.0

1.2.0.0 https://github.com/aVadim483/MathExecutor

Math expressions calculator with custom operators, functions and variables

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Vadim Shemarov aka aVadim

parser math expression calculator

31/12 2017

v1.1.1

1.1.1.0 https://github.com/aVadim483/MathExecutor

Math expressions calculator with custom operators, functions and variables

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Vadim Shemarov aka aVadim

parser math expression calculator

30/12 2017

v1.1.0

1.1.0.0 https://github.com/aVadim483/MathExecutor

Math expressions calculator with custom operators, functions and variables

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Vadim Shemarov aka aVadim

parser math expression calculator

22/10 2017

v1.0.1

1.0.1.0 https://github.com/aVadim483/MathExecutor

Math expressions calculator with custom operators, functions and variables

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Vadim Shemarov aka aVadim

parser math expression calculator

22/10 2017

v1.0.0

1.0.0.0 https://github.com/aVadim483/MathExecutor

Simple math expressions calculator

  Sources   Download

MIT

The Requires

 

The Development Requires

by Vadim Shemarov aka aVadim

parser math expression calculator

16/10 2017

v0.4

0.4.0.0 https://github.com/aVadim483/MathExecutor

Simple math expressions calculator

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Vadim Shemarov aka aVadim

parser math expression calculator

15/10 2017

v0.3

0.3.0.0 https://github.com/aVadim483/MathExecutor

Simple math expressions calculator

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Vadim Shemarov aka aVadim

parser math expression calculator

10/03 2017

v0.2

0.2.0.0 http://github.com/NeonXP/MathExecutor

Simple math expressions calculator

  Sources   Download

MIT

parser math expression calculator

06/09 2013

v0.1

0.1.0.0 http://github.com/NeonXP/MathExecutor

Simple math expressions calculator

  Sources   Download

GPLv2

parser math expression calculator