i have issue with comparing NAN value in C++, Visualstudio.
I need to handle Division by Zero in my code.
if i get division by zero, i want to assign NAN to the result.
Check at later point of time, whether result has NAN.
But NAN comparision is failing at later point of time, even though i assign quiet_Nan() as below.
double d = std::numeric_limits<double>::quiet_NaN();
if( d == std::numeric_limits<double>::quiet_NaN())
{
cout<<" NAN ";
}
else
{
cout<<" Number";
}
I know that floating values can’t be compared for equality. I tried taking diff between d and quiet_Nan() and tried to compare it with floating number with < operator.
I saw few posts but couldn’t get how to compare double value.
how to know the existence of NAN value in a double variable ?
I don’t think the current standard library provides any is_nan implementation you will need to use a 3rd party lib or roll your own
floating point values can be compared for equality, it is just rarely useful, but this is a one of those places where it is useful, what you want is something like:
which checks that you have an appropriate NaN value on your machine and then uses the fact that NaN != NaN is true!
boost propbably should provided this somewhere?