Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c#
infinity
communitycreator
double

How to check if a double value equals infinity in C#

Theodore Kelechukwu Onyejiaku

Overview

In C#, we use the Double.IsInfinity() method to check if a value equals infinity or not.

Syntax

public static bool IsInfinity (double d);
Syntax for Checking If a Double Value is Infinity

Parameters

d: The double value or number we want to check for infinity.

Return value

A Boolean value is returned. It returns true if the d is infinity. Otherwise, it returns false.

Code example

// use System
using System;
// class
class DoubleInfinity
{
    // main method
    static void Main()
    {
        // This will return "true".
        Console.WriteLine("IsInfinity(3.0 / 0) == {0}.", Double.IsInfinity(3.0 / 0) ? "true" : "false");

        // This will return "false"
        Console.WriteLine("IsInfinity(23.4545) == {0}.", Double.IsInfinity(23.4545) ? "true" : "false");
        
        // This will return "true"
        Console.WriteLine("IsInfinity(-2 / 0 ) == {0}.", Double.IsInfinity(-2.0 / 0) ? "true" : "false");
        
        // This will return "false"
        Console.WriteLine("IsInfinity(0.33333333333) == {0}.", Double.IsInfinity(0.33333333333) ? "true" : "false");
    }
}
Check if Double Value is Infinity

Explanation

In the above code, we use the Double.IsInfinity() method to check some values for infinity. We use the ternary operator to check if the method returns true or false, and print the output.

RELATED TAGS

c#
infinity
communitycreator
double

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku
RELATED COURSES

View all Courses

Keep Exploring