Shortest way of checking if Double is "NaN"
.net, c#, floating-point
Solution
As MSDN says, NaN means that result is undefined. With infinities result is defined:
A method or operator returns NaN when the result of an operation is undefined. For example, the result of dividing zero by zero is NaN, as the following example shows. (But note that dividing a non-zero number by zero returns either PositiveInfinity or NegativeInfinity, depending on the sign of the divisor.)
So, it's not good idea to tread infinities as NaN. You can write extension method to check if value is not NaN or infinity:
// Or IsNanOrInfinity
public static bool HasValue(this double value)
{
return !Double.IsNaN(value) && !Double.IsInfinity(value);
}
Problem
When calling `Double.IsNaN()` with `Double.PositiveInfinity` as argument, the result is false. This is against my intuition since infinity is not a number. Apparently "NaN" only exists in terms of a constant in .NET, is this described by the IEEE standard or is it a custom implementation detail? Is there a shorter way to check if a `Double` is "NaN" than: ``` (Double.IsNaN(d) || Double.IsPositiveInfinity(d) || Double.IsNegativeInfinity(d)) ``` or ``` (Double.IsNaN(d) || Double.IsInfinity(d)) ```