Class DoubleEvaluator
java.lang.Object
studio.magemonkey.codex.util.eval.javaluator.AbstractEvaluator<Double>
studio.magemonkey.codex.util.eval.javaluator.DoubleEvaluator
- All Implemented Interfaces:
EvaluationContext
An evaluator that is able to evaluate arithmetic expressions on real numbers.
Built-in operators:
Built-in operators:
- +: Addition
- -: Subtraction
- -: Unary minus
- *: Multiplication
- /: Division
- ^: Exponentiation.
Warning: Exponentiation is implemented using java.lang.Math.pow which has some limitations (please read oracle documentation about this method to known details).
For example (-1)^(1/3) returns NaN. - %: Modulo
- abs: absolute value
- acos: arc cosine
- asin: arc sine
- atan: arc tangent
- average: average of arguments
- ceil: nearest upper integer
- cos: cosine
- cosh: hyperbolic cosine
- floor: nearest lower integer
- ln: natural logarithm (base e)
- log: base 10 logarithm
- max: maximum of arguments
- min: minimum of arguments
- round: nearest integer
- sin: sine
- sinh: hyperbolic sine
- sum: sum of arguments
- tan: tangent
- tanh: hyperbolic tangent
- random: pseudo-random number (between 0 and 1)
- e: Base of natural algorithms
- pi: Ratio of the circumference of a circle to its diameter
- Author:
- Jean-Marc Astesana
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumThe order or operations (operator precedence) is not clearly defined, especially between the unary minus operator and exponentiation operator (see http://en.wikipedia.org/wiki/Order_of_operations). -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final FunctionReturns the absolute value of a numberstatic final FunctionReturns the trigonometric arc-cosine of an angle.static final FunctionReturns the trigonometric arc-sine of an angle.static final FunctionReturns the trigonometric arc-tangent of an angle.static final FunctionReturns the average of n numbers (n>=1)static final FunctionReturns the smallest integer >= argumentstatic final FunctionReturns the trigonometric cosine of an angle.static final FunctionReturns the hyperbolic cosine of a number.static final OperatorThe division operator.static final ConstantA constant that represents e (2.718281...)static final OperatorThe exponentiation operator.static final FunctionReturns the largest integer <= argumentstatic final FunctionReturns the natural logarithm of a numberstatic final FunctionReturns the decimal logarithm of a numberstatic final FunctionReturns the maximum of n numbers (n>=1)static final FunctionReturns the minimum of n numbers (n>=1)static final OperatorThe substraction operator.static final OperatorThe modulo operator.static final OperatorThe multiplication operator.static final OperatorThe negate unary operator in the standard operator precedence.static final OperatorThe negate unary operator in the Excel like operator precedence.static final ConstantA constant that represents pi (3.14159...)static final OperatorThe addition operator.static final FunctionReturns a pseudo random numberstatic final FunctionReturns the closest integer of a numberstatic final FunctionReturns the trigonometric sine of an angle.static final FunctionReturns the hyperbolic sine of a number.static final FunctionReturns the square root of a numberstatic final FunctionReturns the sum of n numbers (n>=1)static final FunctionReturns the trigonometric tangent of an angle.static final FunctionReturns the hyperbolic tangent of a number.Fields inherited from class studio.magemonkey.codex.util.eval.javaluator.AbstractEvaluator
constants, expressionBrackets, functionArgumentSeparator, functionBrackets, functions, operators, tokenizer -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected Doubleevaluate(Constant constant, EvaluationContext evaluationContext) Evaluates a constant.protected Doubleevaluate(Function function, Iterator<Double> arguments, EvaluationContext evaluationContext) Evaluates a function.protected Doubleevaluate(Operator operator, Iterator<Double> operands, EvaluationContext evaluationContext) Evaluates an operation.static ParametersGets a copy of DoubleEvaluator standard default parameters.static ParametersGets a copy of DoubleEvaluator default parameters.protected DoubletoValue(Token literalTok, EvaluationContext evaluationContext) Evaluates a literal (Converts it to a value).Methods inherited from class studio.magemonkey.codex.util.eval.javaluator.AbstractEvaluator
doFunction, evaluate, evaluate, getArguments, getBracketPair, getConstants, getFunctions, getOperators, output, tokenizeMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface studio.magemonkey.codex.util.eval.javaluator.EvaluationContext
getError, getError
-
Field Details
-
PI
A constant that represents pi (3.14159...) -
E
A constant that represents e (2.718281...) -
CEIL
Returns the smallest integer >= argument -
FLOOR
Returns the largest integer <= argument -
ROUND
Returns the closest integer of a number -
ABS
Returns the absolute value of a number -
SINE
Returns the trigonometric sine of an angle. The angle is expressed in radian. -
COSINE
Returns the trigonometric cosine of an angle. The angle is expressed in radian. -
TANGENT
Returns the trigonometric tangent of an angle. The angle is expressed in radian. -
ACOSINE
Returns the trigonometric arc-cosine of an angle. The angle is expressed in radian. -
ASINE
Returns the trigonometric arc-sine of an angle. The angle is expressed in radian. -
ATAN
Returns the trigonometric arc-tangent of an angle. The angle is expressed in radian. -
SINEH
Returns the hyperbolic sine of a number. -
COSINEH
Returns the hyperbolic cosine of a number. -
TANGENTH
Returns the hyperbolic tangent of a number. -
MIN
Returns the minimum of n numbers (n>=1) -
MAX
Returns the maximum of n numbers (n>=1) -
SUM
Returns the sum of n numbers (n>=1) -
AVERAGE
Returns the average of n numbers (n>=1) -
LN
Returns the natural logarithm of a number -
LOG
Returns the decimal logarithm of a number -
SQRT
Returns the square root of a number -
RANDOM
Returns a pseudo random number -
NEGATE
The negate unary operator in the standard operator precedence. -
NEGATE_HIGH
The negate unary operator in the Excel like operator precedence. -
MINUS
The substraction operator. -
PLUS
The addition operator. -
MULTIPLY
The multiplication operator. -
DIVIDE
The division operator. -
EXPONENT
The exponentiation operator. -
MODULO
The modulo operator.
-
-
Constructor Details
-
DoubleEvaluator
public DoubleEvaluator()Constructor.
This default constructor builds an instance with all predefined operators, functions and constants. -
DoubleEvaluator
Constructor.
This constructor can be used to reduce the set of supported operators, functions or constants, or to localize some function or constant's names.- Parameters:
parameters- The parameters of the evaluator.
-
-
Method Details
-
getDefaultParameters
Gets a copy of DoubleEvaluator standard default parameters.
The returned parameters contains all the predefined operators, functions and constants.
Each call to this method create a new instance of Parameters.- Returns:
- a Paramaters instance
- See Also:
-
getDefaultParameters
Gets a copy of DoubleEvaluator default parameters.
The returned parameters contains all the predefined operators, functions and constants.
Each call to this method create a new instance of Parameters.- Returns:
- a Paramaters instance
-
toValue
Description copied from class:AbstractEvaluatorEvaluates a literal (Converts it to a value).- Specified by:
toValuein classAbstractEvaluator<Double>- Parameters:
literalTok- The literal to evaluate.evaluationContext- The context of the evaluation- Returns:
- an instance of T.
-
evaluate
Description copied from class:AbstractEvaluatorEvaluates a constant.
Subclasses that support constants must override this method. The default implementation throws a RuntimeException meaning that implementor forget to implement this method while creating a subclass that accepts constants.- Overrides:
evaluatein classAbstractEvaluator<Double>- Parameters:
constant- The constantevaluationContext- The context of the evaluation- Returns:
- The constant's value
-
evaluate
protected Double evaluate(Operator operator, Iterator<Double> operands, EvaluationContext evaluationContext) Description copied from class:AbstractEvaluatorEvaluates an operation.
Subclasses that support operators must override this method. The default implementation throws a RuntimeException meaning that implementor forget to implement this method while creating a subclass that accepts operators.- Overrides:
evaluatein classAbstractEvaluator<Double>- Parameters:
operator- The operatoroperands- The operandsevaluationContext- The context of the evaluation- Returns:
- The result of the operation
-
evaluate
protected Double evaluate(Function function, Iterator<Double> arguments, EvaluationContext evaluationContext) Description copied from class:AbstractEvaluatorEvaluates a function.
Subclasses that support functions must override this method. The default implementation throws a RuntimeException meaning that implementor forget to implement this method while creating a subclass that accepts functions.- Overrides:
evaluatein classAbstractEvaluator<Double>- Parameters:
function- The functionarguments- The function's argumentsevaluationContext- The context of the evaluation- Returns:
- The result of the function
-