While writing some test cases, and some of the tests check for the result of a NaN.
I tried using std::isnan
but the assert failes:
Assertion `std::isnan(x)' failed.
After printing the value of x
, it turned out it's negative NaN (-nan
) which is totally acceptable in my case.
After trying to use the fact that NaN != NaN
and using assert(x == x)
, the compiler does me a 'favor' and optimises the assert away.
Making my own isNaN
function is being optimised away as well.
How can I check for both equality of NaN and -NaN?
isNaN
, and perhaps the built-in one from your compiler if you have it? One way to test for a NaN (there are several, as you noticed) is to test the bit pattern at en.wikipedia.org/wiki/NaN (exponent is 11..11). – Sifuentesbool isNaN(float x) { return x != x; }
– Rouncestd::isnan
to me. – Goldofpleasureassert(std::isnan(x))
. Printingx
before the assert shows-nan
. – Rouncex != x
tofalse
whenx
has typefloat
, I'm afraid you have to report this as a bug in your compiler. – Sifuentesisnan
should return true for all values ofnan
. Your example demonstrates behavior contrary to that. That's a bug. – Goldofpleasure