I am using Javascript engine in java to calculate the value of the math expression that I insert as a String. Here is the code:
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
String infix = "(2^2+5*2+3)%13";
Double F = (Double) engine.eval(infix);
int f = F.intValue();
System.out.println(f);`
However, it gives me 0, and the correct solution would be 4. I understand this is because of the ^, because when I tried to write the expression like this "(2*2+5*2+3)%13", I got the correct result. So, my question is how to get power of a number using javascript eval?
Aucun commentaire:
Enregistrer un commentaire