Class DoubleEvaluator

java.lang.Object
com.fathzer.soft.javaluator.AbstractEvaluator<Double>
com.fathzer.soft.javaluator.DoubleEvaluator

public class DoubleEvaluator extends AbstractEvaluator<Double>
An evaluator that is able to evaluate arithmetic expressions on real numbers.
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
Built-in functions:
  • 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)
Built-in constants:
  • e: Base of natural algorithms
  • pi: Ratio of the circumference of a circle to its diameter
Author:
Jean-Marc Astesana
See Also:
  • Field Details

    • PI

      public static final Constant PI
      A constant that represents pi (3.14159...)
    • E

      public static final Constant E
      A constant that represents e (2.718281...)
    • CEIL

      public static final Function CEIL
      Returns the smallest integer >= argument
    • FLOOR

      public static final Function FLOOR
      Returns the largest integer <= argument
    • ROUND

      public static final Function ROUND
      Returns the closest integer of a number
    • ABS

      public static final Function ABS
      Returns the absolute value of a number
    • SINE

      public static final Function SINE
      Returns the trigonometric sine of an angle. The angle is expressed in radian.
    • COSINE

      public static final Function COSINE
      Returns the trigonometric cosine of an angle. The angle is expressed in radian.
    • TANGENT

      public static final Function TANGENT
      Returns the trigonometric tangent of an angle. The angle is expressed in radian.
    • ACOSINE

      public static final Function ACOSINE
      Returns the trigonometric arc-cosine of an angle. The angle is expressed in radian.
    • ASINE

      public static final Function ASINE
      Returns the trigonometric arc-sine of an angle. The angle is expressed in radian.
    • ATAN

      public static final Function ATAN
      Returns the trigonometric arc-tangent of an angle. The angle is expressed in radian.
    • SINEH

      public static final Function SINEH
      Returns the hyperbolic sine of a number.
    • COSINEH

      public static final Function COSINEH
      Returns the hyperbolic cosine of a number.
    • TANGENTH

      public static final Function TANGENTH
      Returns the hyperbolic tangent of a number.
    • MIN

      public static final Function MIN
      Returns the minimum of n numbers (n>=1)
    • MAX

      public static final Function MAX
      Returns the maximum of n numbers (n>=1)
    • SUM

      public static final Function SUM
      Returns the sum of n numbers (n>=1)
    • AVERAGE

      public static final Function AVERAGE
      Returns the average of n numbers (n>=1)
    • LN

      public static final Function LN
      Returns the natural logarithm of a number
    • LOG

      public static final Function LOG
      Returns the decimal logarithm of a number
    • RANDOM

      public static final Function RANDOM
      Returns a pseudo random number
    • NEGATE

      public static final Operator NEGATE
      The negate unary operator in the standard operator precedence.
    • NEGATE_HIGH

      public static final Operator NEGATE_HIGH
      The negate unary operator in the Excel like operator precedence.
    • MINUS

      public static final Operator MINUS
      The substraction operator.
    • PLUS

      public static final Operator PLUS
      The addition operator.
    • MULTIPLY

      public static final Operator MULTIPLY
      The multiplication operator.
    • DIVIDE

      public static final Operator DIVIDE
      The division operator.
    • EXPONENT

      public static final Operator EXPONENT
      The exponentiation operator.
    • MODULO

      public static final Operator MODULO
  • Constructor Details

    • DoubleEvaluator

      public DoubleEvaluator()
      Constructor.
      This default constructor builds an instance with all predefined operators, functions and constants.
    • DoubleEvaluator

      public DoubleEvaluator(Parameters parameters)
      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

      public DoubleEvaluator(Parameters parameters, boolean supportsScientificNotation)
      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

      public static Parameters 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

      public static Parameters getDefaultParameters(DoubleEvaluator.Style style)
      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

      protected Iterator<String> tokenize(String expression)
      Description copied from class: AbstractEvaluator
      Converts 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:
      tokenize in class AbstractEvaluator<Double>
      Parameters:
      expression - The expression that is evaluated
      Returns:
      A string iterator.
    • isScientificNotation

      public static boolean isScientificNotation(String str)
    • toValue

      protected Double toValue(String literal, Object evaluationContext)
      Description copied from class: AbstractEvaluator
      Evaluates a literal (Converts it to a value).
      Specified by:
      toValue in class AbstractEvaluator<Double>
      Parameters:
      literal - The literal to evaluate.
      evaluationContext - The context of the evaluation
      Returns:
      an instance of T.
    • evaluate

      protected Double evaluate(Constant constant, Object evaluationContext)
      Description copied from class: AbstractEvaluator
      Evaluates a constant.
      Subclasses that support constants must override this method (The default implementation throws a UnsupportedOperationException).
      Overrides:
      evaluate in class AbstractEvaluator<Double>
      Parameters:
      constant - The constant
      evaluationContext - The context of the evaluation
      Returns:
      The constant's value
    • evaluate

      protected Double evaluate(Operator operator, Iterator<Double> operands, Object evaluationContext)
      Description copied from class: AbstractEvaluator
      Evaluates an operation.
      Subclasses that support operators must override this method (The default implementation throws a UnsupportedOperationException).
      Overrides:
      evaluate in class AbstractEvaluator<Double>
      Parameters:
      operator - The operator
      operands - The operands
      evaluationContext - The context of the evaluation
      Returns:
      The result of the operation
    • evaluate

      protected Double evaluate(Function function, Iterator<Double> arguments, Object evaluationContext)
      Description copied from class: AbstractEvaluator
      Evaluates a function.
      Subclasses that support functions must override this method (The default implementation throws a UnsupportedOperationException).
      Overrides:
      evaluate in class AbstractEvaluator<Double>
      Parameters:
      function - The function
      arguments - The function's arguments
      evaluationContext - The context of the evaluation
      Returns:
      The result of the function