As I understand it NaN's are represented by all 1's in the exponent part of a double's representation and non-zero fraction. (Info from here)
Is it safe to test for NaN using something like:
double num = 1.0;
// Do something to make num NaN
double* pointer = #
unsigned long long num2 = *((unsigned long long*)pointer);
if((unsigned long long)num2 == 0x7FF8000000000001)
{
// Got a NaN
}
I hope that 0x7FF8000000000001 is the correct test.
Since the fraction has to be non-zero to represent a NaN, then I guess the fractional part of the double representation can by anything, so perhaps a more sophisticated test would be required, which converted the fractional part to 1, as shown in the number 0x7FF8000000000001. (Note the 1 on the end, in the fractional part.)
So, would something like this ever be safe to use?
Edit: As Mike mentioned, in the example I should have used a pointer to get the bits, I will make this change now...