patterncppCritical
Checking if a double (or float) is NaN in C++
Viewed 0 times
doublenanfloatchecking
Problem
Is there an isnan() function?
PS.: I'm in MinGW (if that makes a difference).
PS.: I'm in MinGW (if that makes a difference).
Solution
According to the IEEE standard, NaN values have the odd property that comparisons involving them are always false. That is, for a float f,
Note that, as some comments below have pointed out, not all compilers respect this when optimizing code.
For any compiler which claims to use IEEE floating point, this trick should work. But I can't guarantee that it will work in practice. Check with your compiler, if in doubt.
f != f will be true only if f is NaN. Note that, as some comments below have pointed out, not all compilers respect this when optimizing code.
For any compiler which claims to use IEEE floating point, this trick should work. But I can't guarantee that it will work in practice. Check with your compiler, if in doubt.
Context
Stack Overflow Q#570669, score: 410
Revisions (0)
No revisions yet.