Double.IsNaN() Method in C#

The Double.IsNaN() method in C# is used to determine whether a specified double value is "Not a Number" (NaN). This static method returns true if the value represents NaN, and false otherwise. NaN values typically result from invalid mathematical operations like dividing zero by zero.

Syntax

Following is the syntax for the Double.IsNaN() method −

public static bool IsNaN(double val);

Parameters

  • val − A double-precision floating-point number to test.

Return Value

Returns true if the value is NaN; otherwise, false.

Double.IsNaN() Results Valid Numbers 42.5, -10.0 ?, -? ? false NaN Values 0.0 / 0.0 Math.Sqrt(-1) ? true

Using IsNaN() with Infinity Values

It's important to note that infinity values are valid numbers, not NaN. The following example demonstrates this −

using System;

public class Demo {
   public static void Main() {
      double d = 1.0 / 0.0;
      Console.WriteLine("Double Value = " + d);
      Console.WriteLine("HashCode of Double Value = " + d.GetHashCode());
      TypeCode type = d.GetTypeCode();
      Console.WriteLine("TypeCode of Double Value = " + type);
      Console.WriteLine("Positive Infinity? = " + Double.IsInfinity(d));
      Console.WriteLine("Check whether the specified value is NaN? = " + Double.IsNaN(d));
   }
}

The output of the above code is −

Double Value = ?
HashCode of Double Value = 2146435072
TypeCode of Double Value = Double
Positive Infinity? = True
Check whether the specified value is NaN? = False

Using IsNaN() with NaN Values

When an invalid mathematical operation produces NaN, the method correctly identifies it −

using System;

public class Demo {
   public static void Main() {
      double d = 0.0 / 0;
      Console.WriteLine("Double Value = " + d);
      Console.WriteLine("HashCode of Double Value = " + d.GetHashCode());
      TypeCode type = d.GetTypeCode();
      Console.WriteLine("TypeCode of Double Value = " + type);
      Console.WriteLine("Positive Infinity? = " + Double.IsInfinity(d));
      Console.WriteLine("Check whether the specified value is NaN? = " + Double.IsNaN(d));
   }
}

The output of the above code is −

Double Value = NaN
HashCode of Double Value = -524288
TypeCode of Double Value = Double
Positive Infinity? = False
Check whether the specified value is NaN? = True

Common Use Cases

The following example shows practical scenarios where IsNaN() is useful for validation −

using System;

public class Demo {
   public static void Main() {
      double[] values = { 42.5, Double.PositiveInfinity, 0.0/0.0, Math.Sqrt(-1), -10.7 };
      
      foreach (double val in values) {
         Console.WriteLine($"Value: {val}, IsNaN: {Double.IsNaN(val)}");
      }
   }
}

The output of the above code is −

Value: 42.5, IsNaN: False
Value: ?, IsNaN: False
Value: NaN, IsNaN: True
Value: NaN, IsNaN: True
Value: -10.7, IsNaN: False

Conclusion

The Double.IsNaN() method is essential for validating double values in mathematical operations. It returns true only for NaN values resulting from invalid operations like 0/0, while infinity values and regular numbers return false.

Updated on: 2026-03-17T07:04:36+05:30

776 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements