在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.");