Tip: Testing For NaN

I was working on a project today and I was getting NaN as a return value for a trace up until a certain point in time. I figured that a quick check in an if/else statement would solve my issue but I was wrong. Here I will outline what I initially did and the quick and easy fix for it.

My initial code was this:

Actionscript:
  1. var per:Number = ($percent == NaN) ? 0 : ($percent * 100);

That will throw you the following compiler error (even though your app will still run): Warning: 1098: Illogical comparison with NaN. This statement always evaluates to false.

I was a little puzzled so I quickly loaded up the handy ActionScript Error Repository and searched for 1098. Unfortunately, as you can see, error 1098 is not the error I was getting. I don't know if the error is on Adobe's part or on the site's part but I went back to the main page and searched for Compiler Warnings around 1098. I then realized I was no longer on the error site and actually on the Adobe site for Compiler Warnings which listed my issue under 1099. Good job on that one, Adobe!

Anyway, as you can see under the listing for 1099, you can use the global isNaN() method to do the check properly. The revised code is as follows:

Actionscript:
  1. var per:Number = (isNaN($percent)) ? 0 : ($percent * 100);

I'm a bit embarrassed that I didn't know about the isNaN() method in the first place but hopefully this saves someone some time in the future and keeps me aware of those pesky little global methods.

If you found this post useful, please consider leaving a comment, subscribing to the feed, or making a small donation.

10 Comments

Yeah it happens... But really unless you go digging real deep into the documentation there is a TON of stuff in there that I am sure a lot of AS3 developers are not even aware exist, or still exist.

Here is a few examples....

the "include" keyword, that lets you basically add another script to your current script.

or parseInt() and parseFloat() pass in a string returns an integer or floating type (Number)

or that the with() statement is still around from the AS2 days.

or that you can do some crazy syntax like this to call a function "this["funcName"]();"

or that you can create package level functions
package{ public function foo(); }

or that adding the "dynamic" keyword to a class allows you to add properties to an object on the fly

or that you can make a object of a wildcard type by doing something like "var foo:*;"

or all the attributes AS3 has like [Embed ], [Event ] etc...

So many gems inside, but we never know they are there until we need them (or think we need them) 😛

Good one concerning the NaN! I had the same problem a few weeks ago, thanks a lot.

Haha, I know that NaN nightmare for preloaders – it's actually interesting where it comes from: dividing by zero. When you compute bytesLoaded / bytesTotal and bytesTotal equals 0 as Flash doesn't know the size yet (it will never happen when SWF is tested locally though).

So instead isNaN() you could use if (bytesTotal == 0) return;

Indeed.

By the way, you were getting the error because NaN isn't "equal" to anything.

Even NaN != NaN.

Useful little snippet, but as 0g2t says, i'd think using bytesTotal == 0 is slightly faster.
Thx for sharing 🙂

This is a "common" mistake about NaN, following ActionScript documentation, NaN represent what cannot be represented by Number (NaN = Not a Number) and so it can't be equal to anything, even to NaN (as MrKishi said so).

I personaly generally avoid such migraine generator problems by using the "!" sign in my if/else statements:

var per:Number = !$percent ? 0 : $percent * 100;

should work as well and maybe quicker as I migh have read somewhere that the isNaN() method could be a little bit slow.

Thanks for the tip! I'm grabbing numbers from a user via an input textfield. I needed to make sure they weren't typing in anything other than numbers, so I obviously turned to checking if the value of the textField was equal to NaN.

if (myTextField.text != NaN) { ... }

I was getting the warning and, though it's not an error, it still bugged the crap out of me. I did some searching and bingo! Your page had the answer. I just wanted to drop you a line and let you know you helped me out tonight! 🙂 Thanks!

if (!isNaN(myTextField.text)) { ... } // Ahh... My compiler's happy. 🙂

@Ben: I'm glad I could help, however there is a better way to have your users type in only numbers in a textfield. Use the TextField.restrict() method and set it to 0-9 only. Hope that helps.

Ah, I knew there was something like this out there.

myTextField.restrict = "0-9.\\-";

For my numbers with decimals and negatives. Thanks again! 🙂

I had the same problem. Thanks a lot!

Leave a comment

(required)

(required)