How to find out the existence of a NAN value in a double variable?

I have a problem comparing the NAN value in C ++, Visualstudio. I need to process a section under Zero in my code. if I get division by zero, I want to assign a NAN for the result. Check at a later point in time if the result has a NAN. But NAN comparisons fail at a later point in time, although I assign quiet_Nan () as shown 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 ​​cannot be compared for equality. I tried using diff between d and quiet_Nan () and tried to compare it with a floating-point number with the <operator. I saw a few posts, but couldn't figure out how to compare the double value.

How to find out about the existence of a NAN value in a double variable?

+3
3

, is_nan,

, , , , , , - :

template <T>
bool is_nan(T d)
{
  return std::numeric_limits<T>::has_quiet_NaN && d != d;
}

, NaN , , NaN!= NaN !

boost propbably -?

+3

++, :

if (d == std:: numeric_limits:: quiet_NaN())

:

(STD:: numeric_limits:: quiet_isNaN ())

.

, 'd' - NaN, d == d FALSE

+2

I found your information on the Microsoft documentation page:

http://msdn.microsoft.com/en-us/library/w22adx1s.aspx

Try something like

if(myDouble != myDouble)
{
   // myDouble is NaN
}
else
{
   // myDouble is NOT NaN
}

That should do the trick.

0
source

Source: https://habr.com/ru/post/1760508/


All Articles