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";
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