oat-sa/lib-beeme

Simple mathematical expression/equation parser and calculator.

Maintainers

Details

github.com/oat-sa/lib-beeme

Source

Installs: 125 610

Dependents: 1

Suggesters: 0

Security: 0

Stars: 3

Watchers: 21

Forks: 18

0.2.0 2016-11-03 16:25 UTC

This package is auto-updated.

Last update: 2024-12-09 08:54:56 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.