Simple mathematical expression/equation parser and calculator.

0.1.0 2016-07-12 09:28 UTC

This package is not auto-updated.

Last update: 2024-11-13 20:49:57 UTC


README

Simple mathematical expression parser and calculator based on the great work of Adrean Boyadzhiev.

Install

The recommended way to install Beeme is through composer.

{
    "require": {
        "oat-sa/lib-beeme": "dev-master"
    }
}

Basic Usage

Here is an simple example of evaluation of mathematical expression

<?php

$parser = new \oat\beeme\Parser();
$expression = '1 + 2 * 3 * ( 7 * 8 ) - ( 45 - 10 )';
$result = $parser->evaluate($expression);

echo $result; // 302.000000

Constants

Beeme comes with two built-in constants that are "pi" and "e". You can also use custom constans in your expressions, and give them actual values at runtime. Below, an example with the constant "x" replaced by the integer value 3.

<?php

$parser = new \oat\beeme\Parser();
$expression = '3 + x';
$result = $parser->evaluate(
    $expression,
    ['x' => 3]
);

echo $result; // 6.000000

Functions

Beeme provides a set of unary functions to be used in your expressions. Below is an example of using the "abs" function in an expression.

<?php

$expression = '1 + abs(x)';
$result = $parser->evaluate(
    $expression,
    ['x' => -10]
);

echo $result; // 11.000000

Please see the list of available unary functions available in Beeme. They all map to their PHP built-in equivalent:

TODO

  • Always more unit tests
  • N-ary functions

License

MIT, see LICENSE.