Class Evaluator

java.lang.Object
studio.magemonkey.codex.util.eval.Evaluator

public class Evaluator extends Object
  • Constructor Details

    • Evaluator

      public Evaluator()
  • Method Details

    • eval

      public static double eval(String exp, int method)
      Parameters:
      exp - String math expression.
      method - Evaluation method. Allowed: 0 - internal, 1 - javaluator.
      Returns:
      Evaluated expression.
    • compile

      public static CompiledExpression compile(String expression, String... variables)
      Parses a math expression once, so it can be evaluated many times with different values for its variables. Use this instead of eval(String, int) when the same formula runs repeatedly.

      The syntax is that of the Javaluator evaluator (+ - * / ^ %, unary minus, brackets, min, max, abs, sqrt, the constants pi and e and so on).

      Parameters:
      expression - the expression, such as max(d / 5, d - 4 * x / max(1, t + 8))
      variables - the names of the variables the expression may use. Their values are later given to CompiledExpression.eval(double...) in this order. A name cannot be a function, a constant or a number, cannot contain an operator, bracket, comma or whitespace, and cannot be repeated.
      Returns:
      the compiled expression, which is immutable and thread safe
      Throws:
      IllegalArgumentException - with a message naming the expression, if it has a syntax error, mismatched brackets, an unknown variable or function, a function with the wrong number of arguments, or if a variable name is not usable