Determine A Variable is Double.NaN ?

Published on Monday, June 6, 2011
c#

How do you determine a double variable is Double.NaN ?

A sample:

if (minSelectX != Double.NaN)
{
     DateTime timeMinSelectX = DateTime.FromOADate(minSelectX);
}

The code above is not correct. You will get an error like following:

doubleNaN00

The reason is when minSelectX is NaN, the condition of “if (minSelectX != Double.NaN)” will be still true.

The right code should be:

if(!double.IsNaN(minSelectX))
{
     DateTime timeMinSelectX = DateTime.FromOADate(minSelectX);
}