C# Double NaN

Description

Double NaN represents a value that is not a number (NaN). This field is constant.

Syntax

Double.NaN has the following syntax.


public const double NaN

Example

For example, the result of dividing zero by zero is NaN, as the following example shows.


using System;//  w w  w  .  j a  v a2s  .c  om
public class MainClass{
  public static void Main(String[] argv){  
    double zero = 0.0;
    System.Console.WriteLine("{0} / {1} = {2}", zero, zero, zero/zero);
  }
}

The code above generates the following result.

Example 2

In addition, a method call with a NaN value or an operation on a NaN value returns NaN, as the following example shows.


using System;/*  w  w  w .  j  ava  2s .c o m*/
public class MainClass{
  public static void Main(String[] argv){  
    double nan1 = Double.NaN;
    
    System.Console.WriteLine("{0} + {1} = {2}", 3, nan1, 3 + nan1);
    System.Console.WriteLine("Abs({0}) = {1}", nan1, Math.Abs(nan1));
  }
}

The code above generates the following result.

Example 3

It is not possible to determine whether a value is not a number by using the equality operator to compare it to another value that is equal to NaN. The comparison returns false, as the following example shows.


using System;//ww  w  . j a  v a 2  s  . co m

public class MainClass{
  public static void Main(String[] argv){  

        double result = Double.NaN;
        Console.WriteLine("{0} = Double.Nan: {1}", 
                          result, result == Double.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.");


  }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version