All Projects → neonxp → Mathexecutor

neonxp / Mathexecutor

Licence: mit
Simple math expresions parser and calculator

Projects that are alternatives of or similar to Mathexecutor

Mose
Post exploitation tool for configuration management servers.
Stars: ✭ 67 (+0%)
Mutual labels:  hacktoberfest
Prosecutor Database
An open-source, community oversight dataset of all U.S. Prosecutors. Happy Hacktoberfest 🎃
Stars: ✭ 67 (+0%)
Mutual labels:  hacktoberfest
Genesis
🤖 Warframe Discord Cephalon
Stars: ✭ 67 (+0%)
Mutual labels:  hacktoberfest
Laravel Philips Hue
Laravel Philips Hue package to control your lights with remote support
Stars: ✭ 67 (+0%)
Mutual labels:  hacktoberfest
Oblecto
Oblecto is a media server, which streams media you already own, and is designed to be at the heart of your entertainment experience. It runs on your home server to index and analyze your media such as Movies and TV Shows and presents them in an interface tailored for your media consupmtion needs.
Stars: ✭ 67 (+0%)
Mutual labels:  hacktoberfest
Webargs
A friendly library for parsing HTTP request arguments, with built-in support for popular web frameworks, including Flask, Django, Bottle, Tornado, Pyramid, webapp2, Falcon, and aiohttp.
Stars: ✭ 1,145 (+1608.96%)
Mutual labels:  hacktoberfest
Rsyslog
Development repository for the rsyslog cookbook
Stars: ✭ 66 (-1.49%)
Mutual labels:  hacktoberfest
Gulp Wp Pot
Gulp plugin to generate pot file for WordPress plugins and themes
Stars: ✭ 67 (+0%)
Mutual labels:  hacktoberfest
Op Desafios
Soluções para os desafios (por usuário)
Stars: ✭ 66 (-1.49%)
Mutual labels:  hacktoberfest
Portugol Webstudio
IDE online para o Portugol
Stars: ✭ 67 (+0%)
Mutual labels:  hacktoberfest
Aws C Http
C99 implementation of the HTTP/1.1 and HTTP/2 specifications
Stars: ✭ 67 (+0%)
Mutual labels:  hacktoberfest
Openapi Spring Webflux Validator
🌱 A friendly kotlin library to validate API endpoints using an OpenApi 3.0 and Swagger 2.0 specification
Stars: ✭ 67 (+0%)
Mutual labels:  hacktoberfest
Datetimepicker
React Native date & time picker component for iOS, Android and Windows
Stars: ✭ 1,151 (+1617.91%)
Mutual labels:  hacktoberfest
Action Shopify
Deploy Shopify theme with Theme Kit on GitHub Actions
Stars: ✭ 67 (+0%)
Mutual labels:  hacktoberfest
Ex mustang
✨ A simple, clueless bot
Stars: ✭ 67 (+0%)
Mutual labels:  hacktoberfest
Hacktoberfest2020 Expert
Hacktoberfest 2020. Don't forget to spread love and if you like give me a ⭐️
Stars: ✭ 67 (+0%)
Mutual labels:  hacktoberfest
Phpbu
PHP Backup Utility - Creates and encrypts database and file backups, syncs your backups to other servers or cloud services and assists you monitor your backup process
Stars: ✭ 1,147 (+1611.94%)
Mutual labels:  hacktoberfest
Yii2 Smarty
Yii 2 Smarty Extension.
Stars: ✭ 67 (+0%)
Mutual labels:  hacktoberfest
Activity
⚡️ Activity app for Nextcloud
Stars: ✭ 67 (+0%)
Mutual labels:  hacktoberfest
Ifme
Free, open source mental health communication web app to share experiences with loved ones
Stars: ✭ 1,147 (+1611.94%)
Mutual labels:  hacktoberfest

MathExecutor Tests

A simple and extensible math expressions calculator

Features:

  • Built in support for +, -, *, / and power (^) operators plus ()
  • Logical operators (==, !=, <, <, >=, <=, &&, ||)
  • Built in support for most PHP math functions
  • Conditional If logic
  • Support for user defined operators
  • Support for user defined functions
  • Dynamic variable resolution (delayed computation)
  • Unlimited variable name lengths
  • String support, as function parameters or as evaluated as a number by PHP
  • Exceptions on divide by zero, or treat as zero
  • Unary Plus and Minus (e.g. +3 or -sin(12))
  • Pi ($pi) and Euler's number ($e) support to 11 decimal places
  • Easily extensible

Install via Composer:

composer require nxp/math-executor

Sample usage:

use NXP\MathExecutor;

$executor = new MathExecutor();

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

Functions:

Default functions:

  • abs
  • acos (arccos)
  • acosh
  • arcctg (arccot, arccotan)
  • arcsec
  • arccsc (arccosec)
  • asin (arcsin)
  • atan (atn, arctan, arctg)
  • atan2
  • atanh
  • avg
  • bindec
  • ceil
  • cos
  • cosec (csc)
  • cosh
  • ctg (cot, cotan, cotg, ctn)
  • decbin
  • dechex
  • decoct
  • deg2rad
  • exp
  • expm1
  • floor
  • fmod
  • hexdec
  • hypot
  • if
  • intdiv
  • log (ln)
  • log10 (lg)
  • log1p
  • max
  • min
  • octdec
  • pi
  • pow
  • rad2deg
  • round
  • sec
  • sin
  • sinh
  • sqrt
  • tan (tn, tg)
  • tanh

Add custom function to executor:

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

Function default parameters are not supported at this time.

Operators:

Default operators: + - * / ^

Add custom operator to executor:

use NXP\Classes\Operator;

$executor->addOperator(new Operator(
    '%', // Operator sign
    false, // Is right associated operator
    170, // Operator priority
    function (&$stack)
    {
       $op2 = array_pop($stack);
       $op1 = array_pop($stack);
       $result = $op1->getValue() % $op2->getValue();

       return $result;
    }
));

Logical operators:

Logical operators (==, !=, <, <, >=, <=, &&, ||) are supported, but logically they can only return true (1) or false (0). In order to leverage them, use the built in if function:

if($a > $b, $a - $b, $b - $a)

You can think of the if function as prototyped like:

function if($condition, $returnIfTrue, $returnIfFalse)

Variables:

Variables can be prefixed with the dollar sign ($) for PHP compatibility, but is not required.

Default variables:

$pi = 3.14159265359
$e  = 2.71828182846

You can add your own variables to executor:

$executor->setVar('var1', 0.15)->setVar('var2', 0.22);

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

You can dynamically define variables at run time. If a variable has a high computation cost, but might not be used, then you can define an undefined variable handler. It will only get called when the variable is used, rather than having to always set it initially.

$calculator = new MathExecutor();
$calculator->setVarNotFoundHandler(
    function ($varName) {
        if ($varName == 'trans') {
            return transmogrify();
        }
        return null;
    }
);

Division By Zero Support:

Division by zero throws a \NXP\Exception\DivisionByZeroException by default

try {
    echo $executor->execute('1/0');
} catch (DivisionByZeroException $e) {
    echo $e->getMessage();
}

Or call setDivisionByZeroIsZero

echo $executor->setDivisionByZeroIsZero()->execute('1/0');

If you want another behavior, you can override division operator:

$executor->addOperator("/", false, 180, function($a, $b) {
    if ($b == 0) {
        return null;
    }
    return $a / $b;
});
echo $executor->execute('1/0');

String Support:

Expressions can contain double or single quoted strings that are evaluated the same way as PHP evalutes strings as numbers. You can also pass strings to functions.

echo $executor->execute("1 + '2.5' * '.5' + myFunction('category')");

Extending MathExecutor

You can add operators, functions and variables with the public methods in MathExecutor, but if you need to do more serious modifications to base behaviours, the easiest way to extend MathExecutor is to redefine the following methods in your derived class:

  • defaultOperators
  • defaultFunctions
  • defaultVars

This will allow you to remove functions and operators if needed, or implement different types more simply.

Also note that you can replace an existing default operator by adding a new operator with the same regular expression string. For example if you just need to redefine TokenPlus, you can just add a new operator with the same regex string, in this case '\+'.

Documentation

Full class documentation via PHPFUI/InstaDoc

Future Enhancements

This package will continue to track currently supported versions of PHP.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].