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.
Но что бы не наступить случайно на грабли, лучше использовать c++11 и bool isnan(arg) из cmath.