Friday, August 23, 2013

Float to integer conversion problem

Hi,

I'm using GWT 2.5.1 in our project and noticed a weird problem when converting floats to ints.

The code is basically like this:

            marker.location = (int)(x + markerImage.getWidth() / 2) + "," + (int)(y + markerImage.getHeight() / 2);

When getWidth() returns an odd number, the resulting String is something like "23.5,220", as if there was no rounding to integer.

Next step was to extract those inline calculations to variables, but no change:

            int locX = x + markerImage.getWidth() / );
            int locY = y + markerImage.getHeight() / 2;

            marker.location = locX + "," + locY;

Finally the only thing that fixed the problem was:

            int locX = (int) Math.floor(x + markerImage.getWidth() / 2);
            int locY = (int) Math.floor(y + markerImage.getHeight() / 2);

            marker.location = locX + "," + locY;

Seems that if the Int is used only to build a string, no real conversion from double to int takes place. Or this is somehow optimized away.

Note that this happened only with Chrome (v29), not with Firefox.

I'm using the Closure compiler in this project:

             <draftCompile>false</draftCompile>
             <enableClosureCompiler>true</enableClosureCompiler>
             <style>OBF</style>
             <strict>false</strict>

Is this a known problem?

- Otto

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment