在C#的浮点数计算中,0除以0将得到NaN,正数除以0将得到PositiveInfinity,负数除以0将得到NegativeInfinity。C#中浮点数运算从不引发异常。
double
a = 0 / 0d;
if (double.IsNaN(a)){
//此处判断a为NaN
}
if (double.IsNaN(a)){
//此处判断a为NaN
}
Double.NaN 字段
Double.NaN字段表示不是数字 (NaN) 的值。 此字段为常数。
下面的代码示例演示 NaN 的用法:
Double zero
= 0;
// This condition will return false.
if ((0 / zero) == Double.NaN)
Console.WriteLine("0 / 0 can be tested with Double.NaN.");
else
Console.WriteLine("0 / 0 cannot be tested with Double.NaN; use Double.IsNan() instead.");
// This condition will return false.
if ((0 / zero) == Double.NaN)
Console.WriteLine("0 / 0 can be tested with Double.NaN.");
else
Console.WriteLine("0 / 0 cannot be tested with Double.NaN; use Double.IsNan() instead.");