# Adding two number variables produces NaN?

**URL:** <https://forum.kirupa.com/t/adding-two-number-variables-produces-nan/635795>\
**Category:** programming\
**Created:** [January 9, 2017, 11:54am UTC](https://forum.kirupa.com/t/adding-two-number-variables-produces-nan/635795 "2017-01-09T11:54:40Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![kainev](https://avatars.discourse-cdn.com/v4/letter/k/3ec8ea/32.png) [@kainev](https://forum.kirupa.com/u/kainev)\
**Post date:** [January 9, 2017, 11:54am UTC](https://forum.kirupa.com/t/adding-two-number-variables-produces-nan/635795/1 "2017-01-09T11:54:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tbo](https://avatars.discourse-cdn.com/v4/letter/t/7feea3/32.png) [@tbo](https://forum.kirupa.com/u/tbo)\
**Post date:** [January 9, 2017, 1:17pm UTC](https://forum.kirupa.com/t/adding-two-number-variables-produces-nan/635795/2 "2017-01-09T13:17:44Z")

</div>

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;

---

<div class="post-metadata">

**Author:** ![kainev](https://avatars.discourse-cdn.com/v4/letter/k/3ec8ea/32.png) [@kainev](https://forum.kirupa.com/u/kainev)\
**Post date:** [January 9, 2017, 11:47pm UTC](https://forum.kirupa.com/t/adding-two-number-variables-produces-nan/635795/3 "2017-01-09T23:47:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![linusj](https://avatars.discourse-cdn.com/v4/letter/l/b2d939/32.png) [@linusj](https://forum.kirupa.com/u/linusj)\
**Post date:** [January 10, 2017, 12:14am UTC](https://forum.kirupa.com/t/adding-two-number-variables-produces-nan/635795/4 "2017-01-10T00:14:59Z")

</div>

> [@kainev](#):
>
> getTimer();

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

---

<div class="post-metadata">

**Author:** ![kainev](https://avatars.discourse-cdn.com/v4/letter/k/3ec8ea/32.png) [@kainev](https://forum.kirupa.com/u/kainev)\
**Post date:** [January 10, 2017, 12:46am UTC](https://forum.kirupa.com/t/adding-two-number-variables-produces-nan/635795/5 "2017-01-10T00:46:35Z")

</div>

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… =\

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [January 10, 2017, 1:09am UTC](https://forum.kirupa.com/t/adding-two-number-variables-produces-nan/635795/6 "2017-01-10T01:09:34Z")

</div>

You’re telling me that this trace works?

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

```

---

<div class="post-metadata">

**Author:** ![tbo](https://avatars.discourse-cdn.com/v4/letter/t/7feea3/32.png) [@tbo](https://forum.kirupa.com/u/tbo)\
**Post date:** [January 10, 2017, 1:33am UTC](https://forum.kirupa.com/t/adding-two-number-variables-produces-nan/635795/7 "2017-01-10T01:33:53Z")

</div>

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

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

this.addEventListener(Event.ENTER_FRAME, onGameTick);

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?

---

<div class="post-metadata">

**Author:** ![kainev](https://avatars.discourse-cdn.com/v4/letter/k/3ec8ea/32.png) [@kainev](https://forum.kirupa.com/u/kainev)\
**Post date:** [January 10, 2017, 1:46am UTC](https://forum.kirupa.com/t/adding-two-number-variables-produces-nan/635795/8 "2017-01-10T01:46:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tbo](https://avatars.discourse-cdn.com/v4/letter/t/7feea3/32.png) [@tbo](https://forum.kirupa.com/u/tbo)\
**Post date:** [January 10, 2017, 2:08am UTC](https://forum.kirupa.com/t/adding-two-number-variables-produces-nan/635795/9 "2017-01-10T02:08:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kainev](https://avatars.discourse-cdn.com/v4/letter/k/3ec8ea/32.png) [@kainev](https://forum.kirupa.com/u/kainev)\
**Post date:** [January 10, 2017, 3:29am UTC](https://forum.kirupa.com/t/adding-two-number-variables-produces-nan/635795/10 "2017-01-10T03:29:46Z")

</div>

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!

Thanks for all your help!

---

<div class="post-metadata">

**Author:** ![linusj](https://avatars.discourse-cdn.com/v4/letter/l/b2d939/32.png) [@linusj](https://forum.kirupa.com/u/linusj)\
**Post date:** [January 10, 2017, 9:02am UTC](https://forum.kirupa.com/t/adding-two-number-variables-produces-nan/635795/11 "2017-01-10T09:02:47Z")

</div>

Well, the good thing with Flash and actionscript - it fosters generalist rather than specialist.

-“Did you find the error?”  
-“Yes, I rewrote the code”

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [January 10, 2017, 6:52pm UTC](https://forum.kirupa.com/t/adding-two-number-variables-produces-nan/635795/12 "2017-01-10T18:52:55Z")

</div>

I find that bizarre, too.

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [January 10, 2017, 6:59pm UTC](https://forum.kirupa.com/t/adding-two-number-variables-produces-nan/635795/13 "2017-01-10T18:59:20Z")

</div>

> [@tbo](#):
>
> edit: does anyone know why my line break spacing is not kept in the post?

I think the [code][/code] tags don’t work properly. It seems to work if you use three grave accents (`) before and after your code (if that makes any sense).
