Double.IsFinite() Method in C#

In C#, we can typically check if a double value is finite or not using the Double.IsInfinity() method. The Double.IsInfinity() method returns true if the specified value is positive or negative infinity, and false if the value is finite. You can combine the checks for infinity and NaN to check if a double value is not infinity and not NaN (Not a Number).

Syntax:

It has the following syntax:

public static bool IsFinite (double value);

Return Value: In the event that the value is finite, this method returns true otherwise, it returns false.

C# CODE:

Let us take an example to illustrate the Double.IsFinite() method in C#.

using System;

class Program

{

static void Main()

{

double number = 42.0; // Replace with your double value

bool isFinite = !Double.IsInfinity(number) && !Double.IsNaN(number);

if (isFinite)

{

Console.WriteLine("The number is finite.");

}

else

{

Console.WriteLine("The number is either infinity or NaN.");

}

}

}

Output:

The number is finite.

Example 2:

Let us take another example to illustrate the Double.IsFinite() method in C#.

using System;

class Program

{

static void Main()

{

double[] numbers = { 10.5, Double.PositiveInfinity, Double.NaN, -7.2, Double.NegativeInfinity, 42.0 };

foreach (double number in numbers)

{

Console.WriteLine($"Checking if {number} is finite...");

if (IsFinite(number))

{

Console.WriteLine(" It is a finite number.");

}

else

{

Console.WriteLine(" It is either infinity or NaN.");

}

Console.WriteLine();

}

}

static bool IsFinite(double value)

{

return !Double.IsInfinity(value) && !Double.IsNaN(value);

}

}

Output:

Checking if 10.5 is finite...

It is a finite number.

Checking if Infinity is finite...

It is either infinity or NaN.

Checking if NaN is finite...

It is either infinity or NaN.

Checking if -7.2 is finite...

It is a finite number.

Checking if -Infinity is finite...

It is either infinity or NaN.

Checking if 42 is finite...

It is a finite number.

Example 3:

Let us take another example to illustrate the Double.IsFinite() method in C#.

using System;

class Program

{

static void Main()

{

double[] values = { 15.8, Double.PositiveInfinity, 7.3, Double.NaN, -9.6, Double.NegativeInfinity, 123.45 };

foreach (double value in values)

{

Console.WriteLine($"Checking if {value} is finite...");

if (IsFinite(value))

{

Console.WriteLine(" It is a finite number.");

}

else

{

Console.WriteLine(" It is either infinity or NaN.");

}

Console.WriteLine();

}

}

static bool IsFinite(double number)

{

return !Double.IsInfinity(number) && !Double.IsNaN(number);

}

}

Output:

Checking if 15.8 is finite...

It is a finite number.

Checking if Infinity is finite...

It is either infinity or NaN.

Checking if 7.3 is finite...

It is a finite number.

Checking if NaN is finite...

It is either infinity or NaN.

Checking if -9.6 is finite...

It is a finite number.

Checking if -Infinity is finite...

It is either infinity or NaN.

Checking if 123.45 is finite...

It is a finite number.

Example 4:

// C# program to demonstrate the Double.IsFinite() Method

using System;

using System.Globalization;

class FiniteCheckExample {

            // Main Method

            public static void Main()

            {

                        // Updated value with a different number

                        double updatedValue = 15.75;

                        // Using IsFinite() method

                        bool isFiniteResult = Double.IsFinite(updatedValue);

                        // Displaying the result with updated subheadings

                        Console.WriteLine("Checking finiteness of {0}:", updatedValue);

                        if (isFiniteResult)

                                    Console.WriteLine(" {0} is a finite number", updatedValue);

                        else

                                    Console.WriteLine(" {0} is not finite", updatedValue);

            }

}

Output:

Checking finiteness of 15.75:

15.75 is a finite number

Example 5:

// C# program to demonstrate the

// Double.IsFinite() Method

using System;

class FiniteCheckExample

{

// Main Method

public static void Main()

{

// Declaring and initializing a finite value

double finiteValue = 42.75;

// Using IsFinite() method

bool isFiniteResult = Double.IsFinite(finiteValue);

// Displaying the result with updated subheadings

Console.WriteLine("Checking finiteness of a finite value:");

Console.WriteLine(" Original value: {0}", finiteValue);

if (isFiniteResult)

Console.WriteLine(" The value is finite");

else

Console.WriteLine(" The value is not finite");

// Declaring and initializing a positive infinity

double positiveInfinity = Double.PositiveInfinity;

// Using IsFinite() method

bool isPositiveInfinityFinite = Double.IsFinite(positiveInfinity);

// Displaying the result with updated subheadings

Console.WriteLine("\nChecking finiteness of positive infinity:");

Console.WriteLine(" Original value: {0}", positiveInfinity);

if (isPositiveInfinityFinite)

Console.WriteLine(" The value is finite");

else

Console.WriteLine(" The value is not finite");

}

}

Output:

Checking finiteness of a finite value:

Original value: 42.75

The value is finite

Checking finiteness of positive infinity:

Original value: Infinity

The value is not finite

Example 6:

// C# program to demonstrate the

// Double.IsFinite() Method

using System;

class FiniteCheckExample

{

// Main Method

public static void Main()

{

// Declaring and initializing a finite positive value

double finitePositiveValue = 123.456;

// Using IsFinite() method

bool isFinitePositiveResult = Double.IsFinite(finitePositiveValue);

// Displaying the result with updated subheadings

Console.WriteLine("Checking finiteness of a finite positive value:");

Console.WriteLine(" Original value: {0}", finitePositiveValue);

if (isFinitePositiveResult)

Console.WriteLine(" The value is finite");

else

Console.WriteLine(" The value is not finite");

// Declaring and initializing a finite negative value

double finiteNegativeValue = -987.654;

// Using IsFinite() method

bool isFiniteNegativeResult = Double.IsFinite(finiteNegativeValue);

// Displaying the result with updated subheadings

Console.WriteLine("\nChecking finiteness of a finite negative value:");

Console.WriteLine(" Original value: {0}", finiteNegativeValue);

if (isFiniteNegativeResult)

Console.WriteLine(" The value is finite");

else

Console.WriteLine(" The value is not finite");

}

}

Output:

Checking finiteness of a finite positive value:

Original value: 123.456

The value is finite

Checking finiteness of a finite negative value:

Original value: -987.654

The value is finite

Conclusion:

When it comes to verifying the finiteness of double-precision floating-point numbers in C#, the Double.IsFinite() method comes in useful. This way eliminates situations where a value is both positive and negative infinity and NaN (Not a Number), making it simple for developers to ascertain whether a given value is within the representable range of finite numbers. This functionality is especially helpful when handling user input or mathematical computations where verifying the validity of numerical values is crucial. Its use in confirming the finiteness of various double values, such as finite positives, finite negatives, or even situations involving large exponential values, is demonstrated by the examples given.