We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I've been trying to port a sketch over from processing application to processing.js. Everything is being drawn at inccorect positions. And I've found that when I run cos(radians(theta))
or sin(radians(theta))
in the two different programs I get different results for the same value of theta. Is this a known issue? Is there a way to get around it?
Answers
The difference is because by default Processing uses the float data type and JS mode uses the double data type.
Although it would be possible to change the data type float to double the Processing sin and cos method expect a float so would not work unless you used Math.sin and Math.cos instead, but then it wouldn't work in JavaScript mode.
The short answer is no if you want the code to run in both Java and JavaScript mode, but the actual difference in values is minute and MOST unlikely to cause any noticeable differences between the 2 modes.
As @quark said: Use Math.cos(), Math.sin(), etc. in order to get near results from both Java & JS Modes! =:)
Be careful though that Java Mode is hostile to
double
datatype! :|Also, you're gonna need to suffix all literal fractional values w/
d
, like:.5d
,3.1416d
,1e3d
, etc.! (~~)You could also try something like
floor(cos(angle)*100000)*.00001
which should truncate to less decimal precision and be the same in both programs.Gr8 idea @rbrauer! How about scientific notation too? *-:)
"Java Mode":
"Transpiled JS Mode":
"Regular Java":
"Regular JS":
P.S.: About time to find an avatar for yourself, @rbrauer! <):)