Unfortuantely, floating-point arithmetic in most CPUs and computer languages 
today is based on binary fractions (1/2, 1/4, 1/8, 1/16, ...) not decimal 
fractions (1/10, 1/100, 1/1000, 1/1000,...). There is no ActionScript Number 
(or C++ double) that is EXACTLY equal to 0.1, 0.2, or 0.3 because doing so 
would require an infinite number of binary fractional bits. (Some fractions 
such as 0.5 can be exactly represented because they only need a few binary 
fractional bits.) So you're pretty much always dealing with floating-point 
approximations to the real numbers that you think you're manipulating.

- Gordon

From: [email protected] [mailto:[email protected]] On Behalf 
Of Chris
Sent: Wednesday, November 25, 2009 5:17 PM
To: [email protected]
Subject: Re: [flexcoders] Re: When does 0.2 and 0.1 not equal 0.3?



This is a pretty well known limitation of floating point numbers.

A workaround:
http://joshblog.net/2007/01/30/flash-floating-point-number-errors/


Another option around this is to use the toPrecision() function of Number/Int 
to convert a number to a String and then compare the Strings.

var num1:Number = 0.1;
var num2:Number = 0.2;
var num3:Number = num1 + num2;
trace("num3:"+num3);
trace("is 0.3?"+(num3 == 0.3));
trace("num3.toPrecision()"+num3.toPrecision(3));
trace("is precise 0.3?"+(num3.toPrecision(3) == (0.3).toPrecision(3)));


More info:

http://www.google.com/search?q=floating+point+math+errors
On Wed, Nov 25, 2009 at 5:11 PM, tntomek 
<[email protected]<mailto:[email protected]>> wrote:


So to reliably add 0.1 and 0.2 I need to convert them to ints? This must be a 
joke, its not adding anything complicated.

So is there a Flex wrapper for this? math.add(0.1, 0.2) ?

How can we be sure that 2 numbers we just added equal the true sum?


--- In [email protected]<mailto:flexcoders%40yahoogroups.com>, 
"kidl33t" <kidl...@...> wrote:
>
> I have encountered an odd bug. In the process of creating a little numeric 
> stepper component (a text box with an up/down stepper beside it) I have found 
> an odd rounding error. Starting from 0.0 and adding 0.1 increments, I get the 
> follow console output.
>
> currentNumber: 0 increment: 0.1
> result: 0.1
>
> currentNumber: 0.1 increment: 0.1
> result: 0.2
>
> currentNumber: 0.2 increment: 0.1
> result: 0.30000000000000004
>
> As you can see, .2 + .1 is yielding 0.30000000000000004. This behaviour 
> happens at at many numbers actually.
>
> You can verify this yourself by simply doing a: trace( (0.1 + 0.2) );
>
> The other flex developer at our company can also see this error, so I don't 
> think it's isolated to my box or particular build. Does anyone know anything 
> about this?
>


Reply via email to