fasseg / exp4j

A tiny math expression evaluator for the Java programming language
http://www.objecthunter.net/exp4j/
Apache License 2.0
495 stars 163 forks source link

Implicit multiplication bug #92

Open anunnakian opened 6 years ago

anunnakian commented 6 years ago

If you have the following formula "3x+1" and you disable the implicitMultiplication, I'll have, not an exception, but an error telling you that you have too much operators !!

bvedrenne commented 4 years ago

Current version of source throw exception. @Test public void testImplicitMultiplicationOffVariable() { Expression e = new ExpressionBuilder("3x+1").variable("x").implicitMultiplication(false).build(); e.evaluate(); } this is the exception thrown:

java.lang.IllegalArgumentException: No value has been set for the setVariable 'x'.

shuvamc019 commented 4 years ago

Thanks for the update @bvedrenne . It seems like that exception is only being thrown because the variable x has not been assigned any value. Your test could be rewritten as:

@Test
public void testImplicitMultiplicationOffVariable() {
Expression e = new ExpressionBuilder("3x+1").variable("x").implicitMultiplication(false).build().setVariable("x", 2);
e.evaluate();
}

This would assign x the arbitrary value of 2. However, since implicit multiplication is off, an IllegalArgumentException is still thrown, but this time it says that there is an invalid number of arguments passed to the function, which is a more accurate description of the exception thrown.

Correct me if I'm wrong, but it seems to me like that was the only issue with the code. Now, the correct exception is being thrown.

anunnakian commented 4 years ago

Thanks for the update @bvedrenne and @shuvamc019 for your answer. To my mind, I don't think throwing an exception that says there is an invalid number of arguments passed to the function, because we don't use a function (exp4j function) in our formula 3x+1.

The more accurate to me is to throw an UnknownFunctionOrVariableException. What do you think ?