Tuesday, May 3, 2011

integer and long weird arithmetic

A sample code being worth thousands words, here are my little
findings. I'm aware that using long all the way may have got me out of
trouble, but I wanted to share these weird results that some may
consider as a bug. In hosted mode there is no pb. The comments
contains the content of the popups in the "gwtcompiled" mode. Tested
with gwt 2.1 and 2.2 under Chrome, Firefox4 and IE9:


public class MyApp implements EntryPoint{

public void onModuleLoad() {
long m=0, n=0, o=0, p=0, q=0;
int i = 0;
int j = Integer.MAX_VALUE-1;

while (i < j)i++;
alert("i==j?" + (i==j) + " but i=" + i +" and j=" + j);
//==> i==j?true but i=2147483646 and j=2.147483646E9
// did you notice the "float notation" of j?

n = n + j;
n = n + j;
o = o + i;
o = o + i;
//given i == j we can suppose that n == o??
alert("n="+n + " o="+o);
//firefox, chrome, ie9 ==>n=-4 o=4294967292

p = p + (long)j;
p = p + (long)j;

alert("n="+n + " o="+o + " p="+p);
//firefox, chrome, ie9 ==>n=-4 o=4294967292 p=4294967292
//p == o :) this one was expected.

for(int k = 0; k<2; k++){
m = m + j;
}
//at least m should be equal to n, no?
alert("n=" + n +" m=" + m);
//firefox, chrome, ie9 ==>n=-4 m=4294967292
}

public void alert(String s){
Window.alert(s);
}
}

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment