Double.IsInfinity() Method in C#

The Double.IsInfinity() method in C# is used to determine whether a double-precision floating-point number represents positive or negative infinity. This method returns true if the specified number evaluates to either PositiveInfinity or NegativeInfinity, and false otherwise.

Infinity values in C# occur when mathematical operations exceed the range of double values, such as dividing a non-zero number by zero or performing calculations that result in overflow.

Syntax

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

public static bool IsInfinity(double d);

Parameters

  • d − A double-precision floating-point number to test for infinity.

Return Value

The method returns a bool value −

  • true if the value is PositiveInfinity or NegativeInfinity

  • false if the value is a finite number or NaN

Double.IsInfinity() Return Values Returns true +? (PositiveInfinity) -? (NegativeInfinity) Returns false Finite numbers NaN values How to Create 1.0 / 0.0 ? +? -1.0 / 0.0 ? -? Use Double.IsPositiveInfinity() or Double.IsNegativeInfinity() to test specific infinity types

Using Double.IsInfinity() with Finite Numbers

Example

using System;

public class Demo {
   public static void Main() {
      double d = 5.5;
      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("Is Infinity? = " + Double.IsInfinity(d));
   }
}

The output of the above code is −

Double Value = 5.5
HashCode of Double Value = 1075183616
TypeCode of Double Value = Double
Is Infinity? = False

Using Double.IsInfinity() with Positive Infinity

Example

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("Is Infinity? = " + Double.IsInfinity(d));
   }
}

The output of the above code is −

Double Value = ?
HashCode of Double Value = 2146435072
TypeCode of Double Value = Double
Is Infinity? = True

Testing Both Positive and Negative Infinity

Example

using System;

public class Demo {
   public static void Main() {
      double positiveInf = Double.PositiveInfinity;
      double negativeInf = Double.NegativeInfinity;
      double finite = 42.5;
      double nan = Double.NaN;

      Console.WriteLine("Positive Infinity: " + Double.IsInfinity(positiveInf));
      Console.WriteLine("Negative Infinity: " + Double.IsInfinity(negativeInf));
      Console.WriteLine("Finite Number: " + Double.IsInfinity(finite));
      Console.WriteLine("NaN Value: " + Double.IsInfinity(nan));

      Console.WriteLine("\nSpecific infinity tests:");
      Console.WriteLine("Is Positive Infinity: " + Double.IsPositiveInfinity(positiveInf));
      Console.WriteLine("Is Negative Infinity: " + Double.IsNegativeInfinity(negativeInf));
   }
}

The output of the above code is −

Positive Infinity: True
Negative Infinity: True
Finite Number: False
NaN Value: False

Specific infinity tests:
Is Positive Infinity: True
Is Negative Infinity: True

Conclusion

The Double.IsInfinity() method is essential for validating mathematical calculations that might result in infinite values. It returns true for both positive and negative infinity, making it useful for error checking in numerical computations and preventing invalid calculations from propagating through your program.

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

161 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements