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.12Returns a value of 15.5 multiplied by 3.12.x = 12.5/5.36Returns 12.5 divided by 5.36 and stories the result in variablex.= (x * 3) + 2Returns 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.
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.12Returns a value of 15.5 multiplied by 3.12.x = 12.5/5.36Returns 12.5 divided by 5.36 and stores the result in global variablex.= sin(x * .4)Returns the sine of value in variable x multiplied by 0.4.
Any of the
Mathmethods that take just a single argument or no arguments can be used in equations. This means that you cannot useMath.poworMath.atan2, though you can use the '^' operator for powers andMath.tan.If the equation is invalid and fails to parse, a value of
NaNis returned and a relevant error message is stored inPD.EquationParser.error.Parameters:
Name Type Argument Description eqnstring A simple equation such as '= (15*1.62)/4.5'.
variablesMap <optional>
An optional map containing name/value pairs.
Returns:
Returns the resulting value or
NaNif 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
NaNis returned and a relevant error message is stored inPD.EquationParser.error.Parameters:
Name Type Argument Description nodesArray.<object> A parsed tree of expression nodes.
variablesMap <optional>
An optional map of name/value pairs.
Returns:
Returns the evaluated tree value, or
NaNif 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 standardparseFloat()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.12Returns a value of 15.5 multiplied by 3.12.x = 12.5/5.36Returns 12.5 divided by 5.36 and stores the result in global variablex.= sin(x * .4)Returns the sine of value in variable x multiplied by 0.4.
Any of the
Mathmethods that take just a single argument or no arguments can be used in equations. This means that you cannot useMath.poworMath.atan2, though you can use the '^' operator for powers andMath.tan.If the equation is invalid and fails to parse, a value of
NaNis returned and a relevant error message is stored inPD.EquationParser.error.Parameters:
Name Type Argument Description eqnstring A simple equation such as '= (15*1.62)/4.5'.
variablesMap <optional>
An optional map containing name/value pairs.
Returns:
Returns the resulting value or
NaNif it failed.- Type
- number