Problem with expression result (unexpected result) #296
Replies: 2 comments 2 replies
-
First, you're not precise enough. You should use the builtin Expression expression = new Expression("2*PI*180/PI") Second, that's not how the precision on the |
Beta Was this translation helpful? Give feedback.
-
I did a quick test with EvalEx 3 and there the expression @Test
void testPi() throws EvaluationException, ParseException {
Expression expression = new Expression("2*PI*180/PI");
assertThat(expression.evaluate().getStringValue()).isEqualTo("360");
} The reason is that version 3 has a different default math context. It has a higher precision (68 instead of 7) using the same rounding mode (HALF_EVEN). @Test
public void testPi() {
Expression expression = new Expression("2*PI*180/PI",
new MathContext(68, RoundingMode.HALF_EVEN));
assertEquals(expression.eval().toPlainString(), "360");
} |
Beta Was this translation helpful? Give feedback.
-
Good evening.
I am having a probem with the result of an expression that EvalEX does return...
Specifically, I'm trying to make an Android Toast appear and show the result of an expression by this code:
Expression expression = new Expression("2*" + String.valueOf(Math.PI) + "*180/" + String.valueOf(Math.PI)); expression.setPrecision(13); BigDecimal result = expression.eval(); Toast.makeText(mContext, String.valueOf(result), Toast.LENGTH_SHORT).show();
But it is showing "359.9999999999" as the result, instead of "360". Why is this problem occurring?
I want the expression to always have maximum 13 decimal values. I know I can approximate the BigDecimal result to an Integer and get "360" as the result, but I don't want to do this, I want to get a BigDecimal result with maximum 13 decimal numbers with any expression I try to calculate using this code and this expression to return "360" as the correct result. But I don't understand why is EvalEX implementation showing me this strange result...
I would be very glad if you could help me to find a solution to this problem I'm currently having. Thanks.
Beta Was this translation helpful? Give feedback.
All reactions