We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello,
I have a question to gotoloops for js.eval
How can I say 3 to the power of 2 (3^2)? How can I say 3! or 6 % 2 or squareroot of 81?
Is there a general help on the beast?
thanks!
Best, Chrisir ;-)
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
// for evaluation of math
javax.script.ScriptEngine js1;
//
String textLocal = "3 * 4" ;
void setup() {
js1 = new ScriptEngineManager().getEngineByName("js");
}
void draw() {
textLocal = "4 ^ 2" ;
if (!textLocal.equals("")) {
String test1 = evalJS(this, js1, textLocal);
if (!test1.equals("")) {
println( test1 ) ; // success
}
}
}
//
String evalJS(PApplet pa, ScriptEngine js, String expression) {
js.put("p", pa);
try {
Object obj1=js.eval("" + expression);
return obj1.toString();
}
catch (ScriptException cause) {
//throw new RuntimeException(cause);
return "";
}
} //
//
Answers
JavaScript, Java, C, C++, C# share almost all of their operators.
"4 ^ 2" is in JS as it is in Java: the XOR bitwise operator.
The way to get 4 power 2 is via Math's pow() method:
"Math.pow(4, 2);"
https://developer.Mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/pow??
That didn't answer my question...
what do I put in line 20 above for "Math.pow(4, 2);" and for 6! and 4%3 .... ? And 81 sqrt ?
but thanks...... ;-)
@gotoloop
Or did you mean line 20 ??
Thanks!
ups, I see....
@Chrisir, just like Java, JS doesn't have a factorial operator.
The
!
operator is the logical NOT operator:Complete JS operators: https://developer.Mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators
BtW, do you remember some sketches using ScriptEngine for JS not too long ago?
Reposting ScriptEngine wrapper functions here.
initJSProcessingEnv(), evalJS(), compileJS(), runJS(), invokeJS().
They're about 150 lines. So it's best to paste them in a separate ".pde" tab, away from our sight. :P
And I've also tweaked some factorial JS function from:
http://StackOverflow.com/questions/3959211/fast-factorial-function-in-javascript#3959275
And some sample on how to use it:
To finalize, a full old sketch converted to Nashorn JS:
http://studio.ProcessingTogether.com/sp/pad/export/ro.9G5yFfYFFDXi1
Don't forget to paste the 2 tabs above, since the main tab here depends on them all: >-)
Thank you so much.
To be honest , I wanted a way to eval equations like 3 + 4 for my little ChatBot. Google can answer stuff like 3+4 or 4^3 (4 to the power of 3).
So I tried eval and some of it like 3*4 worked right away.
Now I realized that this eval can do full codes and not only formulas
Is there another way to just evaluate math formulas typed by the user?
Thanks, Chrisir
Yup! Nashorn is a full ECMA5 JS implementation embedded into Java's API. ~O)
And here's a nice trick for wrappers evalJS() & compileJS():
Their last parameter is
String... statements
. It means they can accept an array of strings.Thus we can pass loadStrings() to them. That is, we can load scripts from files. *-:)
My initJSProcessingEnv() transfers all Processing's static API to the JS' global scope.
Therefore we can eval() strings like:
sqrt(9)
,pow(3, 2)
,cos(TAU)
, etc.But watch out: Nashorn got access to the whole Java's API + Processing's API.
It means n1 can type in whole programs for eval() w/ malicious intent and hack the computer! :-SS
initJSProcessingEnv
seems great
Is there a way only eval math formula without the security issue big as a truck?
Gotoloop could you answer this bit for me
Thank you...
Hmm. That's gonna need some study from here:
Maybe I may try out to come up w/ some solution based on it... :-\"