Single.IsInfinity() Method in C# with Example

In C#, Single.IsInfinity(Single) is a Single struct method. This method is used to check whether a specified floating-point value evaluates to either positive infinity or negative infinity or not. While performing some mathematical operations, it is possible to obtain a result that is either positive infinity or negative infinity. For Example: If any positive value is divided by zero, it results in positive infinity.

Syntax: public static bool IsInfinity (float f);

Parameter:
<emf: It is a single-precision floating-point number of type System.Single.



Return Type: This method return a boolean value True, if the specified value evaluates to either positive infinity or negative infinity, otherwise return False.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the
// Single.IsInfinity(Single) 
// Method
using System;
  
class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Dividing a Positive number by zero
        // results in positive infinity.
        float zero = 0.0f;
        float value = 10.0f;
        float result = value / zero;
  
        // Printing result
        Console.WriteLine(result);
  
        // Check result using IsInfinity() Method
        Console.WriteLine(Single.IsInfinity(result));
  
        // Result of any operation that
        // exceeds Single.MaxValue
        // is Positive infintity
        result = Single.MaxValue * 9.25f;
  
        // Printing result
        Console.WriteLine(result);
  
        // Check result using IsInfinity() Method
        Console.WriteLine(Single.IsInfinity(result));
  
        // Result of any operation that
        // is less than Single.MinValue
        // is Negative infintity
        result = Single.MinValue * 9.565f;
  
        // Printing result
        Console.WriteLine(result);
  
        // Check result using IsInfinity() Method
        Console.WriteLine(Single.IsInfinity(result));
    }
}

chevron_right


Output:

Infinity
True
Infinity
True
-Infinity
True

Note: Floating-point operations return PositiveInfinity or NegativeInfinity to signal an overflow condition.

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.