Code Monkey home page Code Monkey logo

math-executor's Introduction

Simple math expressions calculator

Install via Composer

All instructions to install here: https://packagist.org/packages/Z/math-executor

Sample usage:

require "vendor/autoload.php";

$calculator = new \Z\MathExecutor();

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

Functions:

Default functions:

  • sin
  • cos
  • tn
  • asin
  • acos
  • atn
  • min
  • max
  • avg

Add custom function to executor:

$executor->addFunction('abs', function($arg) {
    return abs($arg);
}, 1);

Operators:

Default operators: + - * / ^

Add custom operator to executor:

MyNamespace/ModulusToken.php:

<?php
namespace MyNamespace;

use Z\Classes\Token\AbstractOperator;

class ModulusToken extends AbstractOperator
{
    /**
     * Regex of this operator
     * @return string
     */
    public static function getRegex()
    {
        return '\%';
    }

    /**
     * Priority of this operator
     * @return int
     */
    public function getPriority()
    {
        return 3;
    }

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

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

        return new TokenNumber($result);
    }
}

And adding to executor:

$executor->addOperator('MyNamespace\ModulusToken');

Variables:

Default variables:

$pi = 3.14159265359
$e = 2.71828182846

You can add own variable to executor:

$executor->setVars(array(
    'var1' => 0.15,
    'var2' => 0.22
));

$executor->execute("$var1 + $var2");

math-executor's People

Contributors

zrosenthal avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

west7780

math-executor's Issues

Fatal error: Call to a member function getPriority() on null in C:\xampp\htdocs\math\vendor\nxp\math-executor\src\NXP\Classes\Lexer.php on line 107

I've installed math-executor through Composer, ran some tests on the first code and came to this issue:

Code:
require "vendor/autoload.php";
$calculator = new \NXP\MathExecutor();
print $calculator->execute("1 + 2 * (2 - (4+10))^2 + sin(10)");
print $calculator->execute("(4+10)^2");
Output:
-19.544021110889
Notice: Undefined offset: -1 in C:\xampp\htdocs\math\vendor\nxp\math-executor\src\NXP\Classes\Lexer.php on line 107

Fatal error: Call to a member function getPriority() on null in C:\xampp\htdocs\math\vendor\nxp\math-executor\src\NXP\Classes\Lexer.php on line 107

How can I fix this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.