 # Adding two number variables produces NaN?

Hi,

In my application I’m calculating the time between each frame with the following:

private function onGameTick(e:Event):void
{
timePrevious = timeCurrent;
timeCurrent = getTimer();
delta = (timeCurrent - timePrevious) * 0.001;
}

I’m then just trying to add that to another number variable like so:

private var timePrevious:Number = 0;
private var timeCurrent:Number = 0;
private var delta:Number;
private var testNumber:Number = 0;

private function onGameTick(e:Event):void
{
timePrevious = timeCurrent;
timeCurrent = getTimer();
delta = (timeCurrent - timePrevious) * 0.001;

``````testNumber += delta;
trace(testNumber);
``````

}

I’ve tried casting delta as a Number just to make sure but that doesn’t work… what’s interesting is that I can run:

trace(testNumber+delta);

and that will return the correct result, I just can’t store the result in a variable if any math has been done with the delta variable. I’ve only just gotten back into AS3 so I might be missing some completely fundamental here but I’m clueless at this point so any help is greatly appreciated.

Kind Regards,
Kaine

Anything that is defined as Number starts out as Nan. With any number, i always start out defining it as zero.

private var delta:Number=0;

Thanks for the response tbo.

Yeah, I’ve tried having it defined as well, no difference. Either way it gets assigned a value before it’s used. Like I was saying the interesting thing is that I can trace something that uses it like:

trace(3+delta);

and that works fine, however if I try to use it with the += operator it just goes to NaN.

getTimer returns an int (according to documentation). Maybe that messes things up?

That’s why I tried casting it as a number, i.e Number(delta) but that didn’t work. The delta value that is stored is always a decimal anyway… =\

You’re telling me that this trace works?

``````private function onGameTick(e:Event):void {
timePrevious = timeCurrent;
timeCurrent = getTimer();
delta = (timeCurrent - timePrevious) * 0.001;
trace(testNumber+delta);
}
``````

I believe testNumber is Nan, rather than delta. check to see that you defined testNumber before the enterframe is called. I did test and this does work properly in frame 1 on a flash movie

``````var timePrevious:Number = 0;
var timeCurrent:Number = 0;
var delta:Number=0;
var testNumber:Number = 0;

function onGameTick(e:Event):void
{
timePrevious = timeCurrent;
timeCurrent = getTimer();
delta = (timeCurrent - timePrevious) * 0.001;

testNumber += delta;
trace(testNumber);
}
``````

however if I start with var testNumber:Number; then I get Nan, as Nan+=delta= Nan.
you are correct that delta doesnt matter because its set in that function

edit: does anyone know why my line break spacing is not kept in the post?

Yes TheCanadian, that trace works, however if I do:

testNumber+=delta;
trace(testNumber);

I get NaN.

tbo, I’m defining testNumber as 0 so it’s definitely not NaN.

I tried what you said in a flash movie and it works for me too, but when I put the code in my class it doesn’t work -___-… What’s interesting is that if I do this in my function:

```var timePrevious:Number = 0; private var timeCurrent:Number = 0; private var delta:Number=0;```

function onGameTick(e:Event):void
{
var testNumber:Number = 0;
timePrevious = timeCurrent;
timeCurrent = getTimer();
delta = (timeCurrent - timePrevious) * 0.001;
testNumber += delta;
trace(testNumber);
}

it works fine, however if I declare the variable in the class rather than the function I get NaN.

EDIT:
I created a new project with the code in the main class and it works, so it’s something about my current class in my current project, even though the classes are basically identical.

Perhaps if you post the whole class.

At the point where you have the += problem. What if you just trace testNumber by itself? Or delta, surely one of them must be nan at that time

So I found that timeCurrent was NaN, I just rewrote the class, making sure I did everything correctly and it’s working.

Still find it bizarre that I could do arithmetic in a trace statement and it would produce the correct results however if trying to assign that to a variable it didn’t like it. Oh well, it’s fixed now!