Exactly right. You can google "floating point limitations" to find
several very detailed articles on this issue.

Mark Shepherd
Flex Builder Engineering

--- In [email protected], "Gordon Smith" <[EMAIL PROTECTED]> wrote:
>
> This is a FOL of using types like Number in ActionScript or float/double
> in Java, C, C++, etc. They can't do exact decimal fractions, because
> they store values in binary and can't exactly represent a value like
> 0.99. You'll either need to do rounding or compute in pennies rather
> than dollars in order to avoid fractions.
>  
> - Gordon
> 
> ________________________________
> 
> From: [email protected] [mailto:[EMAIL PROTECTED] On
> Behalf Of wdsnapper
> Sent: Thursday, March 22, 2007 1:10 PM
> To: [email protected]
> Subject: [flexcoders] Flex Number addition math bug?
> 
> 
> 
> I have an application that is showing me stange results from basic
> addition. In short, I have a cart full of items that are all $0.99. 
> Each price is in a Number property of an object. The math is simple.
> It is generating very odd results. If I start with:
> 
> var total:Number = 0;
> var price:Number = 0.99;
> 
> then do:
> 
> total += price;
> total += price;
> total += price;
> 
> You would expect total to be equal to 2.97. I get 2.96999999...
> The next time I add price I get 3.96.
> 
> The following simple application illustrates the problem. Try it. Do
> you see the same results? Am I doing something wrong or is there a
> math bug? I can work around this but would REALLY like to understand
> the root cause.
> 
> <?xml version="1.0" encoding="utf-8"?>
> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml
> <http://www.adobe.com/2006/mxml> "
> layout="absolute" initialize="initIt()">
> <mx:Script>
> <![CDATA[
> private var theNumber:Number = 0;
> private var incrementAmount = 0.99;
> 
> private function initIt() {
> this.numberLbl.text = String(theNumber);
> }
> private function incrementIt():void {
> theNumber += incrementAmount;
> this.numberLbl.text = String(theNumber);
> }
> ]]>
> </mx:Script>
> <mx:Panel x="121" y="113" width="250" height="200" layout="absolute">
> <mx:Button x="125" y="44" label="Increment" click="incrementIt()"/>
> <mx:Label id="numberLbl" x="43" y="44" text="Label" fontSize="12"/>
> </mx:Panel>
> 
> </mx:Application>
>

Reply via email to