All Projects → denissimon → Formula Parser

denissimon / Formula Parser

Licence: mit
Parsing and evaluating mathematical formulas given as strings.

Projects that are alternatives of or similar to Formula Parser

Expressionevaluator
A Simple Math and Pseudo C# Expression Evaluator in One C# File. Can also execute small C# like scripts
Stars: ✭ 194 (+212.9%)
Mutual labels:  parser, math, evaluation, expression
Govaluate
Arbitrary expression evaluation for golang
Stars: ✭ 2,130 (+3335.48%)
Mutual labels:  parsing, evaluation, expression
Mathquill
Easily type math in your webapp
Stars: ✭ 1,968 (+3074.19%)
Mutual labels:  math, formula, expression
Sharpmath
A small .NET math library.
Stars: ✭ 36 (-41.94%)
Mutual labels:  parser, math, expression
Self Attentive Parser
High-accuracy NLP parser with models for 11 languages.
Stars: ✭ 569 (+817.74%)
Mutual labels:  parser, parsing
Formula Parser
Javascript Library parsing Excel Formulas and more
Stars: ✭ 544 (+777.42%)
Mutual labels:  parser, formula
Expr Eval
Mathematical expression evaluator in JavaScript
Stars: ✭ 752 (+1112.9%)
Mutual labels:  parser, math
Ccalc
Scientific calculator in which you can define new constants and functions
Stars: ✭ 19 (-69.35%)
Mutual labels:  parser, expression
Algebraicengine Fraction
a calculating engine~
Stars: ✭ 311 (+401.61%)
Mutual labels:  parser, math
Esprima
ECMAScript parsing infrastructure for multipurpose analysis
Stars: ✭ 6,391 (+10208.06%)
Mutual labels:  parser, parsing
Jkt
Simple helper to parse JSON based on independent schema
Stars: ✭ 22 (-64.52%)
Mutual labels:  parser, parsing
Seafox
A blazing fast 100% spec compliant, self-hosted javascript parser written in Typescript
Stars: ✭ 425 (+585.48%)
Mutual labels:  parser, parsing
Wpf Math
.NET library for rendering mathematical formulae using the LaTeX typsetting style, for the WPF framework
Stars: ✭ 339 (+446.77%)
Mutual labels:  math, formula
Meriyah
A 100% compliant, self-hosted javascript parser - https://meriyah.github.io/meriyah
Stars: ✭ 690 (+1012.9%)
Mutual labels:  parser, parsing
Kgt
BNF wrangling and railroad diagrams
Stars: ✭ 312 (+403.23%)
Mutual labels:  parser, parsing
Fuzi
A fast & lightweight XML & HTML parser in Swift with XPath & CSS support
Stars: ✭ 894 (+1341.94%)
Mutual labels:  parser, parsing
Algebra Latex
Parse and calculate latex formatted math
Stars: ✭ 20 (-67.74%)
Mutual labels:  parser, math
Hx Mathparser
Evaluates math expressions. Written in Haxe.
Stars: ✭ 7 (-88.71%)
Mutual labels:  parser, math
Errorstacks
Tiny library to parse error stack traces
Stars: ✭ 29 (-53.23%)
Mutual labels:  parser, parsing
Nearley
📜🔜🌲 Simple, fast, powerful parser toolkit for JavaScript.
Stars: ✭ 3,089 (+4882.26%)
Mutual labels:  parser, parsing

Formula Parser

Latest Stable Version Total Downloads License

Formula Parser is a library for parsing and evaluating mathematical formulas given as strings.

Supports:

  • 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.

To install with Composer:

composer require denissimon/formula-parser

Usage

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:

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

The initialized object $parser has the following methods:

setValidVariables($array) Overwrites default valid variables.

setVariables($array) Sets variables.

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.

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

More usage examples can be found in tests/FormulaParserTest.php

License

Licensed under the MIT license

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].