Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Single.IsInfinity() Method in C# with Example
The Single.IsInfinity() method in C# is used to determine whether a single-precision floating-point number represents positive or negative infinity. This method is particularly useful when performing mathematical operations that might result in infinite values, such as division by zero.
Syntax
Following is the syntax for the Single.IsInfinity() method −
public static bool IsInfinity(float val);
Parameters
val − A single-precision floating-point number to be tested for infinity.
Return Value
The method returns true if the specified value evaluates to positive or negative infinity; otherwise, false.
Using Single.IsInfinity() with Regular Values
This example demonstrates how the method behaves with regular finite values −
using System;
public class Demo {
public static void Main() {
float f1 = 19.3f;
float f2 = Single.MaxValue;
Console.WriteLine("Value1 = " + f1);
Console.WriteLine("Is Value1 infinite? = " + Single.IsInfinity(f1));
Console.WriteLine("\nValue2 = " + f2);
Console.WriteLine("Is Value2 infinite? = " + Single.IsInfinity(f2));
}
}
The output of the above code is −
Value1 = 19.3 Is Value1 infinite? = False Value2 = 3.402823E+38 Is Value2 infinite? = False
Using Single.IsInfinity() with Infinite Values
This example shows how to create infinite values and test them −
using System;
public class Demo {
public static void Main() {
float positiveInfinity = 5.0f / 0.0f;
float negativeInfinity = -5.0f / 0.0f;
float regularValue = Single.MinValue;
Console.WriteLine("Positive Infinity = " + positiveInfinity);
Console.WriteLine("Is positive infinity infinite? = " + Single.IsInfinity(positiveInfinity));
Console.WriteLine("\nNegative Infinity = " + negativeInfinity);
Console.WriteLine("Is negative infinity infinite? = " + Single.IsInfinity(negativeInfinity));
Console.WriteLine("\nRegular Value = " + regularValue);
Console.WriteLine("Is regular value infinite? = " + Single.IsInfinity(regularValue));
}
}
The output of the above code is −
Positive Infinity = ? Is positive infinity infinite? = True Negative Infinity = -? Is negative infinity infinite? = True Regular Value = -3.402823E+38 Is regular value infinite? = False
Common Use Cases
The Single.IsInfinity() method is commonly used in mathematical calculations to −
Validate division operations before performing calculations
Handle edge cases in numerical algorithms
Check results of mathematical operations that might overflow
Implement error handling for floating-point arithmetic
Conclusion
The Single.IsInfinity() method provides a reliable way to detect infinite values in single-precision floating-point numbers. It returns true for both positive and negative infinity, making it essential for robust mathematical operations and error handling in C# applications.
