-
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
addEventListener (Event.ENTER_FRAME,spin)
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
-
that is odd..
try this:
import flash.events.Event;
var speed:Number = 100.0;
addEventListener(Event.ENTER_FRAME, spin);
function spin(e:Event):void {
speed -= 0.1;
trace(int(speed*10)/10);
}
-
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.
-
Thanks both for replies -- I never knew of the floating point issue
great help thanks again
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|