On Feb 24, 1:52 pm, "tony.p.." <tony.t....@gmail.com> wrote:

> This seems like a bug in JS, more than just the way it handles float.
> Because I'm not doing any calculations on the GWT (JS) side, I'm just
> printing what comes from the server. So if 15.2 if printed as
> 15.2000000000, that would be ok, but to be printed as
> 15.199999809265136 is really weired. Am I right or am I missing
> something?

You're missing something.  In JavaScript there is no such number as
15.2.  JavaScript uses IEEE-754 standard floating-point notation,
which can only represent numbers of the form (i times 2 ** j) where **
represents exponentiation, and i and j are integers.  See
http://babbage.cs.qc.edu/courses/cs341/IEEE-754references.html for
details.  The effect is similar to trying to represent 1/3 with an
exact decimal representation; you can come very close but you can't
get it exactly.

If you want to represent exact decimal numbers in JavaScript you have
two choices:  Use strings, and forego doing arithmetic on them, or use
rational numbers, where all numbers are internally in the form (i/j)
with i and j integers.

One very useful subset of rational notation is scaled decimal, where j
is a constant power of 10 depending on the maximum precision you want
to retain.  So if you're maintaining 4 digits after the decimal place,
j is 10000, and 15.2 is represented as 152000/10000.  The denominator
(also called the scale factor) can be implied in this case (i.e. you
don't need to store it with each number; think of it as a static final
value).

This notation makes it easy to add and subtract; just add or subtract
the numerators.  Multiplication is easy if the numbers are small --
multiply the numerators and then divide by the scale factor.  Division
is relatively easy; multiply one numerator by the scale factor and
then divide by the other numerator.  In both multiplication and
division you need to think about rounding, and also about overflow if
any of the intermediate results might exceed 15 digits.

--~--~---------~--~----~------------~-------~--~----~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to