#include <limits>
using namespace std;
const double double_nan = numeric_limits<double>::quiet_NaN();
double x = double_nan;
if (x == double_nan) cout << "OK" << endl; I dont get OK! Why?
#include <limits>
using namespace std;
const double double_nan = numeric_limits<double>::quiet_NaN();
double x = double_nan;
if (x == double_nan) cout << "OK" << endl; I dont get OK! Why?
’s observation is expected behavior. As and noted, IEEE‑754 treats NaN as “unordered”: comparisons with a NaN do not behave like ordinary numbers, so == will be false even when you compare a NaN with itself (see the NaN entry for background).NaN (Wikipedia)
To detect NaNs in portable C++, use the library function rather than relying on equality tricks. Include <cmath> and call std::isnan:
#include <cmath>
if (std::isnan(x)) {
// handle NaN
} std::isnan is the clear, standard way to test for NaN across platforms and compilers.std::isnan (cppreference)
A few practical notes: use std::isfinite / std::isinf / std::fpclassify when you need broader classification of floating values; to check if two values are both NaN test both with std::isnan(a) && std::isnan(b) instead of a == b. The common trick x != x will work to detect NaN but is cryptic—prefer std::isnan for readability and portability. If you explicitly construct NaNs (as in the original example), std::numeric_limits<T>::quiet_NaN() creates a quiet NaN; be aware NaNs typically propagate through arithmetic and can break sorting or comparisons unless handled.numeric_limits::quiet_NaN (cppreference)
(Thanks to for pointing toward searching for isnan; the linked answers here show the concise, standard approach.)
Jump to Post— mattjbond 54#include <limits> using namespace std; const double double_nan = numeric_limits<double>::quiet_NaN(); double x = double_nan; if (x == double_nan) cout << "OK" << endl;I dont get OK! Why?
Because a NAN wont compare to anything, even itself. You'd need to check the IEEE standard on floating point numbers …
Jump to Post— jwenting 1,905Correct. NaN is defined mathematically to not be equal to itself.
#include <limits> using namespace std; const double double_nan = numeric_limits<double>::quiet_NaN(); double x = double_nan; if (x == double_nan) cout << "OK" << endl;I dont get OK! Why?
Because a NAN wont compare to anything, even itself. You'd need to check the IEEE standard on floating point numbers for exactly why. (IEEE-754 I think). Simple as that.
Correct. NaN is defined mathematically to not be equal to itself.
Ok, thanks, this is a convention, its mathematically logical, but not lcical for a programer, because nan is also a bit series as any other number...
Are there any bool function which gives back true if a number is nan?
(ok I can write it, but are there any built in function?)
I found it pretty easy. Check this link.
Thanks a lot, Fbody....There goes my productivity for this morning...
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.