You presumably meant the result should be 23.7, not 23.8.

 

However, getting 23.700000000000003 isn't a bug; you'd get the same answer in other languages like Java and C++. The imprecision is because Number in ActionScript (like float in Java and C++) stores a floating-point value as a binary fraction, not as a decimal fraction. These datatypes use binary fractions because that's how current microprocessors store and manipulate floating-point values.

 

Java solves this problem by having a BigDecimal class whjich can store decimal fractions. In that case, computations are slower because it takes many microsprocessor instructions to do a single operation.

 

The current version of ActionScript doesn't have any decimal fraction datatype, but it is under consideration for a future version of the language. The lack of this feaure comes up about once a week, and some participants on flexcoders have discussed the possibiliy of porting BigDecimal or its equivalent to ActionScript.

 

Perhaps you can simply round your results to the precision you need? Take a look at the toFixed() method of the Number class.

 

- Gordon

 


From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On Behalf Of beloved_zhou
Sent: Wednesday, August 30, 2006 4:22 AM
To: flexcoders@yahoogroups.com
Subject: [flexcoders] Is this a bug? About Number type.

 

I am using Flex2.0, and I try to get a result about 3 multiply 7.9,
the result should be 23.8, but I got 23.700000000000003.

Here is a sample code:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute">
<mx:TextInput x="76" y="348" id="txt_1" text="3"/>
<mx:TextInput x="291" y="348" id="txt_2" text="7.9"/>
<mx:TextInput x="560" y="348" id="txt_3"/>
<mx:Button x="474" y="348" label="Button" click="doIt()"/>
<mx:Script>
<![CDATA[
private var num1:Number = 3;
private var num2:Number = 7.9;

private function doIt():void
{
txt_3.text = String(Number(txt_1.text) * Number(txt_2.text));
trace(num1 * num2);
}
]]>
</mx:Script>
</mx:Application>

__._,_.___

--
Flexcoders Mailing List
FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.com





SPONSORED LINKS
Software development tool Software development Software development services
Home design software Software development company


YAHOO! GROUPS LINKS




__,_._,___

Reply via email to