다음을 통해 공유


Double.NaN 필드

정의

숫자가 아닌 값을 나타냅니다(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 합니다. 예를 들어 다음 예제와 같이 0을 0으로 나눈 결과는 입니다 NaN. 그러나 0이 아닌 숫자를 0으로 나누면 수위의 기호에 따라 또는 NegativeInfinity가 반환됩니다PositiveInfinity.

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 값과 비교 Double.NaN 하는 데 사용할 수 없지만 비교 메서드(예: EqualsCompareTo)는 사용할 수 있습니다. 다음 예제에서는 비교 연산자와 메서드 간의 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

적용 대상

추가 정보