GWT-client math vs. java-math

202 views
Skip to first unread message

Oskar Stangenberg

unread,
Aug 12, 2014, 12:09:55 PM8/12/14
to google-we...@googlegroups.com
Hi guys, we are developing a GWT-project with a lot of shared files. We discovered a few discrepancies in the gwt-math and server-math and wanted to share them for anybody else having those issues:
  • First and foremost: all native double calculations are identical (+ - * / %)
  • All trig-functions (sin,cos,tan,asin,acos,atan,atan2) seem to produces slightly different results. We recommend doing your own impl using lookup-maps
  • sqrt,log and pow seem to be identical although we only tested sqrt thoroughly
  • As far as we know all functions like max,min,abs etc are identical
  • toDegrees and toRadian are actually different because GWT uses a constant for 180./PI and PI/180. which changes the order of operation compared to java
  • For serialization of doubles we strongly suggest using Double.doubleToLongbits and Double.longBitsToDouble. You can additionally use Long.toString(long,32) and Long.parseString(string,32) to compress the String-representation
I hope we can help some people with this info. Especially the toDegrees/toRadians was very surprising to find. This issue was reported though and might be fixed in a future GWT-version (we tested 2.6.1).
In case you are interested how we solved the issues here is the open-source-part of our project so you can check it out
Regards
Oskar Stangenberg

Juan Pablo Gardella

unread,
Aug 12, 2014, 1:45:08 PM8/12/14
to google-we...@googlegroups.com
Thanks for sharing. It will very useful track each problem to GWT issue tracker.


--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-tool...@googlegroups.com.
To post to this group, send email to google-we...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

Oskar Stangenberg

unread,
Aug 14, 2014, 8:36:29 AM8/14/14
to google-we...@googlegroups.com
Well, not every one of these things is easy to fix. Especially the trig-functions - GWT is probably using the js-internal ones - which are most likely pretty fast and good enough for client-only code. 
The server-impl of the trig-functions isn't even java-code - they are native functions. So their exact impl might even depend on the JRE used


Am Dienstag, 12. August 2014 19:45:08 UTC+2 schrieb Juan Pablo Gardella:
Thanks for sharing. It will very useful track each problem to GWT issue tracker.
2014-08-12 13:09 GMT-03:00 Oskar Stangenberg <crok...@gmail.com>:
Hi guys, we are developing a GWT-project with a lot of shared files. We discovered a few discrepancies in the gwt-math and server-math and wanted to share them for anybody else having those issues:
  • First and foremost: all native double calculations are identical (+ - * / %)
  • All trig-functions (sin,cos,tan,asin,acos,atan,atan2) seem to produces slightly different results. We recommend doing your own impl using lookup-maps
  • sqrt,log and pow seem to be identical although we only tested sqrt thoroughly
  • As far as we know all functions like max,min,abs etc are identical
  • toDegrees and toRadian are actually different because GWT uses a constant for 180./PI and PI/180. which changes the order of operation compared to java
  • For serialization of doubles we strongly suggest using Double.doubleToLongbits and Double.longBitsToDouble. You can additionally use Long.toString(long,32) and Long.parseString(string,32) to compress the String-representation
I hope we can help some people with this info. Especially the toDegrees/toRadians was very surprising to find. This issue was reported though and might be fixed in a future GWT-version (we tested 2.6.1).
In case you are interested how we solved the issues here is the open-source-part of our project so you can check it out
Regards
Oskar Stangenberg

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsub...@googlegroups.com.

Colin Alworth

unread,
Aug 14, 2014, 5:26:30 PM8/14/14
to google-we...@googlegroups.com
Thanks for posting about this Oskar. The radian/degree code can likely be changed (is that behavior specified as part of the Java spec?), though changing RPC serialization sounds like it would come with performance penalties - any comments there?

What is your preferred method of resolving the trig function issues - do you have a gwt-able java lib that you can suggest?

Oskar Stangenberg

unread,
Aug 15, 2014, 7:16:10 AM8/15/14
to google-we...@googlegroups.com
Im not sure but i doubt the radian/degree thing is in the specs. As for the RPC-serialization: I dont know it the long-bits-conversion is really necessary - its more of a feeling that Double.toString doesnt provide the necessary precision in gwt but its very well possible im wrong there. We are using atmosphere for data transfer and our own serializer logic that hands around raw strings until the value is acually needed so the performance impact is minimal
I havent found any libraries for the trig-functions. we are using our own with a lookup-array

Craig Mitchell

unread,
May 19, 2019, 7:19:45 AM5/19/19
to GWT Users
My testing has revealed that Math.sqrt does not yield the same results.

I think GWT just converts Math.sqrt to the JavaScript Math.sqrt function.  I noticed the JavaScript Math.sqrt function returns different results based on what browser, and what operating system you are using.

I couldn't think of a smart way to fix this to return the same result all the time, so I ended up just rounding the answer to 6 decimal places.

Craig Mitchell

unread,
May 19, 2019, 9:10:09 PM5/19/19
to GWT Users
The biggest difference I saw when running shared code between Java and JavaScript, was that JavaScript doesn't support floats (they will be treated as doubles in JavaScript).
Reply all
Reply to author
Forward
0 new messages