Please feel free to add content to this list.
Yes e.g., BigDecimal result = new Expression("0xcafe + 0xbabe").eval();
will be correctly evaluated by EvalEx.
The default math context used in expressions has a precision of 7, which is too small for large numbers. Either create the expression with a different math context, or specify the precision before evaluating:
Expression expression = new Expression("timestamp >= 1500551315569", new MathContext(13));
or
expression.with("timestamp", "1500551315568").setPrecision(13).eval();
When evaluation an expression, e.g.:
String result = new Expression("5*8").eval();
The string value in result will read 4E+1
. The reason is, that Expression.eval() returns a java.math.BigDecimal
and the toString()
method of BigDecimal
uses scientific notation. Use Expression.eval().toPlainString()
instead.
There is a bit confusion among different calculators on how the precedence and association is handled in combination
with the power of operator. Excel, for example, evaluates above expression to 4, but Google calculator -4.
The default precedence for the power of operator in EvalEx is lower than the unary minus operator and therefore,
-2 is calculated first, which results in (-2)*(-2)
, which calculates to 4.
The precedence of the power operator can be adjusted when an expression is created:
ExpressionSettings settings = ExpressionSettings.builder()
.powerOperatorPrecedenceHigher()
.build();
BigDecimal result = new Expression("-2^2", settings).eval();