# puzzled by increment anomoly

• 12-06-2011, 08:16 AM
sleena
puzzled by increment anomoly
Hi

I am trying to increment a number by 0.1 but the return value is slightly out and I have no idea why -- here is code

///////////////////////////////////////////
var speed:Number = 100

function spin(e:Event){

speed -= 0.1
trace (speed)
}
///////////////////////////////////////////
traces

99.9
99.80000000000001
99.70000000000002
99.60000000000002
99.50000000000003
99.40000000000003
99.30000000000004
99.20000000000005
99.10000000000005
////////////////////////////////////////////////////////
I need it to output 99.9, 99.8, 99.7 etc -- I know I could smooth out the
to my requirements, but why is it not returning exact amount?? --

thanks
• 12-06-2011, 11:23 AM
cultcreative
that is odd..

try this:

import flash.events.Event;

var speed:Number = 100.0;

function spin(e:Event):void {
speed -= 0.1;
trace(int(speed*10)/10);
}
• 12-06-2011, 01:48 PM
5TonsOfFlax
It's standard floating point error. Every programming language with a floating point type has this phenomenon.

Number in AS3 is a IEEE-754 double-precision floating-point number. It's got 64 bits in which to store all the information about your numbers. It can use up to 53 of those bits for the integer part, but basically it cannot store infinitely precise numbers.

In your case, if you need perfect representations of .1, over a relatively small range, then I suggest using ints, and scaling everything by 1/10. Or, if it's just the display that's bothering you, just format it with Number's toFixed method.
• 12-07-2011, 08:18 AM
sleena
Thanks both for replies -- I never knew of the floating point issue

great help thanks again