Nathan,

I appreciate your analysis, but this has nothing to do with the
representability of these decimals in binary and everything to do with
the unnecessary casting of float to double prior to string-printing
it. The serialization code just needs to use the String.valueOf method
for floats instead of doubles.  That's all there is to it.  Very easy
to fix, as I explain in the bug report.


1) Incorrect Example (current implementation):

  a). Java side:

String.valueOf((double)28.4f) => "28.399999618530273"

  b). Javascript:

""+parseFloat("28.399999618530273") => "28.399999618530273"

2) Correct Example:

  a). Java side:

String.valueOf(28.4f) => "28.4"

  b). Javascript:

""+parseFloat("28.4") => "28.4"

I ran this code, trust me, it works :)

- Alex

On Thu, Sep 18, 2008 at 1:47 PM, Perelman Nathan (Nathan)
<[EMAIL PROTECTED]> wrote:
>
> I doubt that this will be resolved since all numbers in JavaScript are
> treated as doubles. I'm not really sure what the correct translation of
> floats in client side Java code should be, but just treating them as
> doubles seems like a reasonable solution.
>
> Also, the serializer has no way of knowing what precision you require on
> your values. Note that of your example values, the only one precisely
> representable in base 2 is 28.5. All others just happen to be rounded
> back to representations with a single digit after the decimal point in
> the decimal -> float -> decimal conversion, but not in the decimal ->
> float -> double -> decimal conversion.
>
> For example:
> 28.4 = 0x1C.666666 (6 repeating, cut to 32 bits) =
> 28.39999997615814208984375
>
> -----Original Message-----
> From: Google-Web-Toolkit@googlegroups.com
> [mailto:[EMAIL PROTECTED] On Behalf Of Alex Epshteyn
> Sent: Wednesday, September 17, 2008 20:32
> To: Google Web Toolkit
> Subject: Warning about using float fields with RPC (shocking bug)
>
>
> Amazing but true: only a hanful of all possible float values will be
> serialized correctly on the server end of GWT RPC.
>
> Examples:
>
> 28.4 => 28.399999618530273
> 28.5 => 28.5
> 28.6 => 28.600000381469727
> 28.7 => 28.700000762939453
> 28.9 => 28.899999618530273
>
> Fortunately this should be easy to fix.
>
> See my bug report for more info:
>
> http://code.google.com/p/google-web-toolkit/issues/detail?id=2897
>
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to