Class: EquationParser

PD. EquationParser


new EquationParser()

A class for parsing and evaluating simple mathematical expressions.

If the given string does not contain an = sign, it is parsed as a numeric value using the standard parseFloat() function. If it does, then a mathematical expression is expected, formed from a combination of numbers, variables and operations.

As this parser is intended to assist with quick data entry in input boxes, it does not currently support complex mathematical functions as the system Calculator tool is better suited to that job.

The following are some example valid expressions:

  • = 15.5 * 3.12   Returns a value of 15.5 multiplied by 3.12.
  • x = 12.5/5.36   Returns 12.5 divided by 5.36 and stories the result in variable x.
  • = (x * 3) + 2   Returns value in variable x multiplied by 3, then plus 2.

If the equation is invalid and fails to parse, a value of NaN is returned and a relevant error message is stored in PD.EquationParser.error.

Author:
  • drajmarsh

Members


:string

error <static, readonly>

Stores a description any error(s) that occured during the last parse process.

If this value is an empty string, then no error occured.

Type
  • string

Methods


_parseEquation(eqn [, variables]) <static>

Parses and evaluates a simple mathematical expressions.

As this parser is intended to assist with quick data entry in input boxes, it does not currently support complex mathematical functions as the system calculator tool is better suited to that job.

The following are some example valid expressions:

  • = 15.5 * 3.12   Returns a value of 15.5 multiplied by 3.12.
  • x = 12.5/5.36   Returns 12.5 divided by 5.36 and stores the result in global variable x.
  • = sin(x * .4)   Returns the sine of value in variable x multiplied by 0.4.

Any of the Math methods that take just a single argument or no arguments can be used in equations. This means that you cannot use Math.pow or Math.atan2, though you can use the '^' operator for powers and Math.tan.

If the equation is invalid and fails to parse, a value of NaN is returned and a relevant error message is stored in PD.EquationParser.error.

Parameters:
Name Type Argument Description
eqn string

A simple equation such as '= (15*1.62)/4.5'.

variables Map <optional>

An optional map containing name/value pairs.

Returns:

Returns the resulting value or NaN if it failed.

Type
number

evaluateSyntaxTree(nodes [, variables]) <static>

Evaluates the given syntax tree using the given variables.

This method allows an application to parse an expression using the getSyntaxTree() method and then evaluate it multiple times using local variables with changing values.

If the equation is invalid and fails to parse, a value of NaN is returned and a relevant error message is stored in PD.EquationParser.error.

Parameters:
Name Type Argument Description
nodes Array.<object>

A parsed tree of expression nodes.

variables Map <optional>

An optional map of name/value pairs.

Returns:

Returns the evaluated tree value, or NaN if it failed.

Type
number

parse(eqn [, variables]) <static>

Parses and evaluates simple mathematical expressions.

If the given string does not contain an = sign, it is parsed as a numeric value using the standard parseFloat() function. If it does, then a mathematical expression is expected, formed from a combination of numbers, variables and operations.

As this parser is intended to assist with quick data entry in input boxes, it does not currently support complex mathematical functions as the system Calculator tool is better suited to that job.

The following are some example valid expressions:

  • = 15.5 * 3.12   Returns a value of 15.5 multiplied by 3.12.
  • x = 12.5/5.36   Returns 12.5 divided by 5.36 and stores the result in global variable x.
  • = sin(x * .4)   Returns the sine of value in variable x multiplied by 0.4.

Any of the Math methods that take just a single argument or no arguments can be used in equations. This means that you cannot use Math.pow or Math.atan2, though you can use the '^' operator for powers and Math.tan.

If the equation is invalid and fails to parse, a value of NaN is returned and a relevant error message is stored in PD.EquationParser.error.

Parameters:
Name Type Argument Description
eqn string

A simple equation such as '= (15*1.62)/4.5'.

variables Map <optional>

An optional map containing name/value pairs.

Returns:

Returns the resulting value or NaN if it failed.

Type
number