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 <tnto...@yahoo.com> 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 flexcoders@yahoogroups.com <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