NAN (Not a number)

This post basically deals with C++ and NaN. NaN(Not a Number) is a value of numeric data type representing an undefined or unrepresentable value, especially in floating-point calculations.

I picked this list up from the wiki page.
Different representation of NAN


I have come across NaN and NaN% on Macintosh. I had seen NaN% on 10.4(Tiger) when I had my calculation go bad and ended up dividing a number by zero. That code was written in C++. Recently I saw NAN while debugging a piece of code written in JavaScript, this was on 10.6(Snow Leopard). So most probably representation of NAN has something to do with the operation performed or may be Apple has changed the representation of NAN in Snow Leopard

I had spent few days working on an interface to Web-kit. Where in I was receiving data provided by the java-scripts running on web-kit. I came across the NAN problem.

The code :

var valueInField= parseInt(document.getElementById(“field”).value);
var value = set(valueInField);

Now if the input value to “field” is a string and “parseInt” would convert the value into NAN. Although Nan stands for Not a Number it is represented as floating point number, so my checks went for a toss.

While looking for fix I came across a method defined in math.h for NAN checks.


This function is part of C99, it may not be available everywhere.

According to the IEEE standard, NaN values have the odd property that comparisons involving them are always false. That is, for a float f, f != f will be true only if f is NaN.

So I wrote a piece of code

if (float_val != float_val)

I didn’t have to use this check extensively, but if it has to be done many times it would be good to use it as an inline method

inline bool isnan(double x)
return x != x;

Viusal C++ does not provide neither std::isnan nor std::tr1::isnan, but it provides an extension function defined as _isnan().

On Xcode(Macintosh) it is a little confusing, it not only provides std::isnan but also provides __inline_isnand()(on Intel machines) and __isnand()(on Power PC). Read these post for more clarifications post 1 and post 2.

On Arjun’s request:

I have attached the image showing Nan. The code was debugged using Firebug plug-in for Firefox web browser.


10 thoughts on “NAN (Not a number)

    • Don’t be surprised. If you click on “technical” hyper-link in the “Categories” section on the right side, you would find lots of other technical posts.

    • It is a high praise coming from you. The first case I was talking about occurred when I was testing the second version of LibNetAccess. Might refresh your mind.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s