2017 © Pedro Peláez
 

library formula-parser

Parsing and evaluating mathematical formulas given as strings.

image

denissimon/formula-parser

Parsing and evaluating mathematical formulas given as strings.

  • Wednesday, March 22, 2017
  • by denissimon
  • Repository
  • 4 Watchers
  • 18 Stars
  • 7,698 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 6 Forks
  • 0 Open issues
  • 4 Versions
  • 19 % Grown

The README.md

Formula Parser

Latest Stable Version Total Downloads License, (*1)

Formula Parser is a library for parsing and evaluating mathematical formulas given as strings., (*2)

Supports:, (*3)

  • Operators: +, -, *, /, ^
  • Variables: x, y, z, a, b
  • Numbers with decimal point '.'
  • Numbers in E notation
  • Constants: pi, e, Inf
  • Functions: sqrt, abs, sin, cos, tan, log, exp
  • Unlimited nested parentheses
  • NaN (Not a Number)

Installation

Requires PHP 5.4 or higher., (*4)

To install with Composer:, (*5)

``` sh composer require denissimon/formula-parser, (*6)


Usage ----- ``` php require_once __DIR__ . '/vendor/autoload.php'; use FormulaParser\FormulaParser; $formula = '3*x^2 - 4*y + 3/y'; $precision = 2; // Number of digits after the decimal point try { $parser = new FormulaParser($formula, $precision); $parser->setVariables(['x' => -4, 'y' => 8]); $result = $parser->getResult(); // [0 => 'done', 1 => 16.38] } catch (\Exception $e) { echo $e->getMessage(), "\n"; }

The $precision parameter has a default of 4, and it's not required to specify:, (*7)

php $parser = new FormulaParser('3+4*2/(1-5)^8'); $result = $parser->getResult(); // [0 => 'done', 1 => 3.0001], (*8)

The initialized object $parser has the following methods:, (*9)

setValidVariables($array) Overwrites default valid variables., (*10)

setVariables($array) Sets variables., (*11)

getResult() Returns an array [0 => v1, 1 => v2], where v1 is 'done' or 'error', and v2 is a computed result or validation error message, respectively., (*12)

getFormula() Returns the text of the formula passed to the constructor., (*13)

More usage examples can be found in tests/FormulaParserTest.php, (*14)

License

Licensed under the MIT license, (*15)

The Versions

22/03 2017

dev-master

9999999-dev

Parsing and evaluating mathematical formulas given as strings.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Denis Simon

parser math expression parsing formula evaluation

28/12 2016

v2.5.0

2.5.0.0

Parsing and evaluating mathematical formulas given as strings.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Denis Simon

parser math expression parsing formula

01/02 2016

v2.4.0

2.4.0.0

Parsing and evaluating mathematical formulas given as strings.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Denis Simon

parser math expression parsing formula

07/08 2015

v2.3.0

2.3.0.0

Parsing and evaluating mathematical formula entered as a string.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Denis Simon

parser math expression formula