In C#, we use the Double.IsInfinity()
method to check if a value equals infinity or not.
public static bool IsInfinity (double d);
d
: The double value or number we want to check for infinity.
A Boolean value is returned. It returns true
if the d
is infinity. Otherwise, it returns false
.
// use Systemusing System;// classclass DoubleInfinity{// main methodstatic 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");}}
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.