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 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.