This content has been marked as final.
Show 5 replies

1. Re: addition problem  is actionscript/flash able to calculate correctly????
Newsgroup_User May 4, 2006 10:16 AM (in response to ed_skinner)"ed_skinner" <webforumsuser@macromedia.com> wrote in message news:e3dcfr$irm$1@forums.macromedia.com...
>
> Hi,
> I am adding two float numbers that return an incorrect result. Here is
> what I am doing...
>
> var simpleMath:Number = Number(parseFloat("110.50")) 
> Number(parseFloat("107.10"));
> alert("Simple Math=" + String(simpleMath));
>
> The result I get is... Simple Math=3.40000000000001
>
> How is this possible?
It's the same old problem we have always had with floating point stored in computer memory.. not just Flash. For dealing with
dollars and cents you can make it easy on yourself by multiplying both sides by 100 and rounding to eliminate decimals, then in
final calculation divide by 100.
You can also multiply/divide by 1000 if you need higher precision in the pennies.
tralfaz

2. Re: addition problem  is actionscript/flash able to calculate correctly????
Newsgroup_User May 4, 2006 3:46 PM (in response to ed_skinner)That is why most commercial programs store currency as whole numbers of
cents, rather than as floating point numbers (which by their very nature
cannot be exact)
You need to change your program to take the nature of numbers in computers
into account to avoid errors. The bug is really in how you are doing your
calculations, not in Flash, and the solution also lies with you.

Jeckyl

3. Re: addition problem  is actionscript/flash able to calculate correctly????
kglad May 4, 2006 6:59 PM (in response to ed_skinner)whoa, there's jedi in the jeckyl. who knew? 
4. Re: addition problem  is actionscript/flash able to calculate correctly????
ed_skinner May 5, 2006 8:10 AM (in response to ed_skinner)Hey Thanks guys. I used the technique of *100, calculate, round and then /100 to get it to work right. One might think that in all the advancement in computers that floating point numbers would be handled correctly by now!
Oh well, past another problem with the help of fellow programmers.
Folks... and may the force be with you! 
5. Re: addition problem  is actionscript/flash able to calculate correctly????
Newsgroup_User May 5, 2006 6:29 PM (in response to ed_skinner)It doesn't matter how advanced a computer it is .. 0.1 (for example) can not
be expressed exactly in binary (which computers use) .. no matter what you
do, it is always only an approximation (a bit like 1/3 =
0.3333333333333333.... in decimal). Its just the mathematics of it.
Fixed point arithmetic (where you implicitly multiply by some number.. like
multiply by 100 for currency amounts to give you cents) is often done. For
instance, the x and y coordinates in Flash are internally fixed point
numbers (multiples of 1/20 = 0.05)

Jeckyl