ExprEval

Simple algebraic expression evaluator with arbitrary precision

Version: $Date: 2009/08/02 08:09:39 $

PHP version: 5

Required modules: standard, spl, simplexml, dom, pcre

Required packages: BigFloat.php, BigInt.php

Author: icosaedro.it di Umberto Salsi <salsi@icosaedro.it>

Source code and updates: <a href="http://www.icosaedro.it/bignumbers/">www.icosaedro.it/bignumbers</a>

This package demostrates how the BigFloat class can be used to evaluate simple algebraic expressions. For example, input fields of a WEB form can be made clever enough to accept not only bare numbers, but also complete expressions.



class ExprEval

{


static BigFloat value(
        string $expr,
        int $precision)

Evaluate a simple algebraic expression

An expression can be as simple as a fractional number, but arithmetic operators + - /, the remainder operator % and sub-expressions are also allowed. Spaces between numbers and operators are ignored. Calculations are performed with the class BigFloat.

The syntax of an expression, spaces apart, can be expressed in EBNF notation as follows:

      expr = ["+"|"-"] term { ("+"|"-") term };
      term = factor { (""|"/"|"%") factor };
      factor = BigFloat | "(" expr ")";
  

Examples:

      ExprEval::value("1/3", -5)  gives 0.33333
      ExprEval::value("2  (0.57 - 0.56)", -2) gives 0.02
  

Parameters:
$expr   The algebraic expression to be evaluated.
$precision   Values resulting from divisions are rounded to 10e$precision so to get two decimal digits $precision must be set to -2.

Return: The result of the evaluation.

Throws:

}

Private items

ExprScanner

Generated by PHPLint Documentator