The C99 spec requires that math.h should define the constant float value NAN. However, NAN isn’t defined in the Visual Studio version of math.h, so you have to define it yourself (VS only implements C89). It’s pretty straight-forward for 32-bit machines… here’s my implementation with cross-platform protection:
#ifdef WIN32
#ifndef NAN
static const unsigned long __nan[2] = {0xffffffff, 0x7fffffff};
#define NAN (*(const float *) __nan)
#endif
#endif
This code is adapted from an MSDN example.


NAN is only defined in C99, MSVC doesn’t implement that yet.
In C++ you should be able to use numeric_limits::quiet_NaN() instead.
Reply
Tom reply on April 20th, 2011 10:26 am:
Thanks Dan! You are correct. I’ll remove the reference to C++. I was writing a C99-compatible library and needed NAN defined.
Reply
I found the following easier and independent of 32/64 bit platform issues.
It also avoids the __nan constant:
#ifdef _MSC_VER
#define INFINITY (DBL_MAX+DBL_MAX)
#define NAN (INFINITY-INFINITY)
#endif
The check for a defined _MSC_VER makes sure that the code can even
be compiled on a C99 compliant compiler or on Unix/Linux.
Reply