Double.NaN 欄位
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
代表不是數字 (NaN
) 的值。 這個欄位為常數。
public: double NaN = NaN;
public const double NaN = NaN;
val mutable NaN : double
Public Const NaN As Double = NaN
欄位值
Value = 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." );
}
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.");
let zero = 0.
// This condition will return false.
if 0. / zero = Double.NaN then
printfn "0 / 0 can be tested with Double.NaN."
else
printfn "0 / 0 cannot be tested with Double.NaN use Double.IsNan() instead."
Dim zero As Double = 0
' This condition will return false.
If (0 / zero) = Double.NaN Then
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.")
End If
備註
當工作的結果未定義時,方法或運算子會 NaN 傳回 。 例如,將零除以零的結果為 NaN,如下列範例所示。 (不過,根據除數的符號,將非零的數位除以零會傳回 PositiveInfinity 或 NegativeInfinity,視除數的符號而定。)
double zero = 0.0;
Console.WriteLine("{0} / {1} = {2}", zero, zero, zero/zero);
// The example displays the following output:
// 0 / 0 = NaN
let zero = 0.0
printfn $"{zero} / {zero} = {zero / zero}"
// The example displays the following output:
// 0 / 0 = NaN
Dim zero As Double = 0
Console.WriteLine("{0} / {1} = {2}", zero, zero, zero/zero)
' The example displays the following output:
' 0 / 0 = NaN
此外,具有 NaN 值或值作業的方法 NaN 呼叫會傳回 NaN,如下列範例所示。
double nan1 = Double.NaN;
Console.WriteLine("{0} + {1} = {2}", 3, nan1, 3 + nan1);
Console.WriteLine("Abs({0}) = {1}", nan1, Math.Abs(nan1));
// The example displays the following output:
// 3 + NaN = NaN
// Abs(NaN) = NaN
let nan1 = Double.NaN
printfn $"{3} + {nan1} = {3. + nan1}"
printfn $"abs({nan1}) = {abs nan1}"
// The example displays the following output:
// 3 + NaN = NaN
// abs NaN = NaN
Dim nan1 As Double = Double.NaN
Console.WriteLine("{0} + {1} = {2}", 3, nan1, 3 + nan1)
Console.WriteLine("Abs({0}) = {1}", nan1, Math.Abs(nan1))
' The example displays the following output:
' 3 + NaN = NaN
' Abs(NaN) = NaN
IsNaN使用 方法來判斷值是否不是數位。 運算子 Equality 會將兩 NaN 個值視為不相等。 一般而言,Double運算符無法與其他值進行比較Double.NaNDouble,不過比較方法 (例如 Equals 和 CompareTo) 。 下列範例說明比較運算符和方法之間的 Double 行為差異。
using System;
public class Example
{
public static void Main()
{
Console.WriteLine("NaN == NaN: {0}", Double.NaN == Double.NaN);
Console.WriteLine("NaN != NaN: {0}", Double.NaN != Double.NaN);
Console.WriteLine("NaN.Equals(NaN): {0}", Double.NaN.Equals(Double.NaN));
Console.WriteLine("! NaN.Equals(NaN): {0}", ! Double.NaN.Equals(Double.NaN));
Console.WriteLine("IsNaN: {0}", Double.IsNaN(Double.NaN));
Console.WriteLine("\nNaN > NaN: {0}", Double.NaN > Double.NaN);
Console.WriteLine("NaN >= NaN: {0}", Double.NaN >= Double.NaN);
Console.WriteLine("NaN < NaN: {0}", Double.NaN < Double.NaN);
Console.WriteLine("NaN < 100.0: {0}", Double.NaN < 100.0);
Console.WriteLine("NaN <= 100.0: {0}", Double.NaN <= 100.0);
Console.WriteLine("NaN >= 100.0: {0}", Double.NaN > 100.0);
Console.WriteLine("NaN.CompareTo(NaN): {0}", Double.NaN.CompareTo(Double.NaN));
Console.WriteLine("NaN.CompareTo(100.0): {0}", Double.NaN.CompareTo(100.0));
Console.WriteLine("(100.0).CompareTo(Double.NaN): {0}", (100.0).CompareTo(Double.NaN));
}
}
// The example displays the following output:
// NaN == NaN: False
// NaN != NaN: True
// NaN.Equals(NaN): True
// ! NaN.Equals(NaN): False
// IsNaN: True
//
// NaN > NaN: False
// NaN >= NaN: False
// NaN < NaN: False
// NaN < 100.0: False
// NaN <= 100.0: False
// NaN >= 100.0: False
// NaN.CompareTo(NaN): 0
// NaN.CompareTo(100.0): -1
// (100.0).CompareTo(Double.NaN): 1
open System
printfn $"NaN = NaN: {Double.NaN = Double.NaN}"
printfn $"NaN <> NaN: {Double.NaN <> Double.NaN}"
printfn $"NaN.Equals(NaN): {Double.NaN.Equals Double.NaN}"
printfn $"not (NaN.Equals NaN): {not (Double.NaN.Equals Double.NaN)}"
printfn $"IsNaN: {Double.IsNaN Double.NaN}"
printfn $"\nNaN > NaN: {Double.NaN > Double.NaN}"
printfn $"NaN >= NaN: {Double.NaN >= Double.NaN}"
printfn $"NaN < NaN: {Double.NaN < Double.NaN}"
printfn $"NaN < 100.0: {Double.NaN < 100.0}"
printfn $"NaN <= 100.0: {Double.NaN <= 100.0}"
printfn $"NaN >= 100.0: {Double.NaN > 100.0}"
printfn $"NaN.CompareTo(NaN): {Double.NaN.CompareTo Double.NaN}"
printfn $"NaN.CompareTo(100.0): {Double.NaN.CompareTo 100.0}"
printfn $"(100.0).CompareTo(Double.NaN): {(100.0).CompareTo Double.NaN}"
// The example displays the following output:
// NaN = NaN: False
// NaN <> NaN: True
// NaN.Equals(NaN): True
// not (NaN.Equals NaN): False
// IsNaN: True
//
// NaN > NaN: False
// NaN >= NaN: False
// NaN < NaN: False
// NaN < 100.0: False
// NaN <= 100.0: False
// NaN >= 100.0: False
// NaN.CompareTo(NaN): 0
// NaN.CompareTo(100.0): -1
// (100.0).CompareTo(Double.NaN): 1
Module Example
Public Sub Main()
Console.WriteLine("NaN = NaN: {0}", Double.NaN = Double.NaN)
Console.WriteLine("NaN <> NaN: {0}", Double.NaN <> Double.NaN)
Console.WriteLine("NaN.Equals(NaN): {0}", Double.NaN.Equals(Double.NaN))
Console.WriteLine("Not NaN.Equals(NaN): {0}", Not Double.NaN.Equals(Double.NaN))
Console.WriteLine("IsNaN: {0}", Double.IsNaN(Double.NaN))
Console.WriteLine()
Console.WriteLine("NaN > NaN: {0}", Double.NaN > 100.0)
Console.WriteLine("NaN >= NaN: {0}", Double.NaN >= 100.0)
Console.WriteLine("NaN < NaN: {0}", Double.NaN < Double.NaN)
Console.WriteLine("NaN < 100.0: {0}", Double.NaN < 100.0)
Console.WriteLine("NaN <= 100.0: {0}", Double.NaN <= 100.0)
Console.WriteLine("NaN >= 100.0: {0}", Double.NaN > 100.0)
Console.WriteLine("NaN.CompareTo(NaN): {0}", Double.NaN.CompareTo(Double.Nan))
Console.WriteLine("NaN.CompareTo(100.0): {0}", Double.NaN.CompareTo(100.0))
Console.WriteLine("(100.0).CompareTo(Double.NaN): {0}", (100.0).CompareTo(Double.NaN))
End Sub
End Module
' The example displays the following output:
' NaN == NaN: False
' NaN != NaN: True
' NaN.Equals(NaN): True
' ! NaN.Equals(NaN): False
' IsNaN: True
'
' NaN > NaN: False
' NaN >= NaN: False
' NaN < NaN: False
' NaN < 100.0: False
' NaN <= 100.0: False
' NaN >= 100.0: False
' NaN.CompareTo(NaN): 0
' NaN.CompareTo(100.0): -1
' (100.0).CompareTo(Double.NaN): 1