Package com.fathzer.soft.javaluator
Class DoubleEvaluator
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 sum of n numbers (n>=1)static final FunctionReturns the trigonometric tangent of an angle.static final FunctionReturns the hyperbolic tangent of a number. -
Constructor Summary
ConstructorsConstructorDescriptionConstructor.DoubleEvaluator(Parameters parameters) Constructor.DoubleEvaluator(Parameters parameters, boolean supportsScientificNotation) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionprotected DoubleEvaluates a constant.protected DoubleEvaluates a function.protected DoubleEvaluates an operation.static ParametersGets a copy of DoubleEvaluator standard default parameters.static ParametersGets a copy of DoubleEvaluator default parameters.static booleanConverts the evaluated expression into tokens.protected DoubleEvaluates a literal (Converts it to a value).Methods inherited from class com.fathzer.soft.javaluator.AbstractEvaluator
evaluate, evaluate, getConstants, getFunctions, getOperators, guessOperator, validateHomonyms
-
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 -
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.
-
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.supportsScientificNotation- true to support scientific number notation (false is the default).
Please note that supporting scientific number notation makes the evaluator twice as slow.
-
-
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 Parameters 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.- Parameters:
style- The operator precedence style of teh evaluator.- Returns:
- a Parameters instance
-
tokenize
Description copied from class:AbstractEvaluatorConverts the evaluated expression into tokens.
Example: The result for the expression "-1+min(10,3)" is an iterator on "-", "1", "+", "min", "(", "10", ",", "3", ")".
By default, the operators symbols, the brackets and the function argument separator are used as delimiter in the string.- Overrides:
tokenizein classAbstractEvaluator<Double>- Parameters:
expression- The expression that is evaluated- Returns:
- A string iterator.
-
isScientificNotation
-
toValue
Description copied from class:AbstractEvaluatorEvaluates a literal (Converts it to a value).- Specified by:
toValuein classAbstractEvaluator<Double>- Parameters:
literal- 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 UnsupportedOperationException).- Overrides:
evaluatein classAbstractEvaluator<Double>- Parameters:
constant- The constantevaluationContext- The context of the evaluation- Returns:
- The constant's value
-
evaluate
Description copied from class:AbstractEvaluatorEvaluates an operation.
Subclasses that support operators must override this method (The default implementation throws a UnsupportedOperationException).- Overrides:
evaluatein classAbstractEvaluator<Double>- Parameters:
operator- The operatoroperands- The operandsevaluationContext- The context of the evaluation- Returns:
- The result of the operation
-
evaluate
Description copied from class:AbstractEvaluatorEvaluates a function.
Subclasses that support functions must override this method (The default implementation throws a UnsupportedOperationException).- Overrides:
evaluatein classAbstractEvaluator<Double>- Parameters:
function- The functionarguments- The function's argumentsevaluationContext- The context of the evaluation- Returns:
- The result of the function
-