9 Replies Latest reply on May 26, 2009 3:41 PM by Francisc

Why is:

0.96 + 0.13 = 1.0899999999999999

Surely it should be exactly 1.09.

0.96 and 0.13 are saved as Number variables then summed up (var1+var2).

• ###### 2. Re: AS and adding

Yep a precision thing.

```<mx:Script>
<![CDATA[
private var myNum1:Number=0.96;

private var myNum2:Number=0.13;

private function test():void{
}
]]>
</mx:Script>
```
• ###### 3. Re: AS and adding

Still I find it odd to have 0.96+0.13=1.09 need precision considering it's not a division operation to actually get more decimals; addition will alwyas keep the same number of decimals as the number with the most decimals or lessen it.

Nevertheless, toPrecision is not a good option for me because I don't know what precision I need.

The 0.13 and 0.96 is something determined not preset.

Is there another way?

• ###### 4. Re: AS and adding

I found the answer in the ECMA specification here, it's kinda complex to explain so I think you'll understand it better if you read it by yourself.

http://www.ecma-international.org/publications/standards/Ecma-262.htm

• ###### 5. Re: AS and adding

Hmm, that would be a good read, but I don't have time for it right now.

I browsed it and while I got some explanations, I didn't find any suitable solutions...

Is there a way to have 0.96+0.13 evaluate correctly?

• ###### 6. Re: AS and adding

(0.96 + 0.13).toFixed(2);

Basically, you have to tell it how many places after the decimal where you want the round off to occur.

You just can express all floating point numbers in the series

A/2 + B/4 + C/8 + D/16 + ... + FF/2^32

where A, B, C, D, ... FF are either 0 or 1

Alex Harui

Flex SDK Developer

• ###### 7. Re: AS and adding

All right, thank you Alex.

I feel this should be addressed in a future update as basic operations should not need such intricate paths to solve.

• ###### 8. Re: AS and adding

AFAIK, you can reproduce this round off error in every platform and operating system.  It is an IEEE standard.  Actionscript is a bit more sensitive to it which is why it provides toFixed(), a decision made by the ECMA standards committee.  I doubt you'll see any changes, but you can always file a bug against the Tamarin project.

Alex Harui

Flex SDK Developer