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.IsNaN() Method in C# with Examples
The Single.IsNaN() method in C# is used to determine whether a specified float value is not a number (NaN). This method returns true if the value is NaN, and false otherwise. NaN typically results from undefined mathematical operations like dividing zero by zero.
Syntax
Following is the syntax for the Single.IsNaN() method −
public static bool IsNaN(float f);
Parameters
-
f − A single-precision floating-point number to be tested.
Return Value
Returns true if the value is NaN (Not a Number), otherwise false.
Using IsNaN() with Different Values
Example 1 - Testing Normal Numbers and Infinity
using System;
public class Demo {
public static void Main() {
float f1 = 5.0f / 0.0f; // Positive infinity
float f2 = 45.5f; // Regular number
Console.WriteLine("Value1 = " + f1);
Console.WriteLine("Is Value1 NaN? = " + Single.IsNaN(f1));
Console.WriteLine("Is Value1 Infinity? = " + Single.IsInfinity(f1));
Console.WriteLine("\nValue2 = " + f2);
Console.WriteLine("Is Value2 NaN? = " + Single.IsNaN(f2));
Console.WriteLine("Is Value2 Infinity? = " + Single.IsInfinity(f2));
}
}
The output of the above code is −
Value1 = ? Is Value1 NaN? = False Is Value1 Infinity? = True Value2 = 45.5 Is Value2 NaN? = False Is Value2 Infinity? = False
Using IsNaN() with Actual NaN Values
Example 2 - Testing NaN Values
using System;
public class Demo {
public static void Main() {
float f1 = 5.0f / 0.0f; // Positive infinity
float f2 = 0.0f / 0.0f; // NaN (undefined operation)
float f3 = Single.NaN; // Direct NaN assignment
Console.WriteLine("Value1 = " + f1);
Console.WriteLine("Is Value1 NaN? = " + Single.IsNaN(f1));
Console.WriteLine("\nValue2 = " + f2);
Console.WriteLine("Is Value2 NaN? = " + Single.IsNaN(f2));
Console.WriteLine("\nValue3 = " + f3);
Console.WriteLine("Is Value3 NaN? = " + Single.IsNaN(f3));
}
}
The output of the above code is −
Value1 = ? Is Value1 NaN? = False Value2 = NaN Is Value2 NaN? = True Value3 = NaN Is Value3 NaN? = True
Common Use Cases
The Single.IsNaN() method is commonly used to validate floating-point calculations and handle edge cases in mathematical operations. It helps prevent runtime errors when dealing with invalid mathematical results.
Example 3 - Input Validation
using System;
public class Calculator {
public static float Divide(float a, float b) {
float result = a / b;
if (Single.IsNaN(result)) {
Console.WriteLine("Warning: Division resulted in NaN");
return 0.0f;
}
else if (Single.IsInfinity(result)) {
Console.WriteLine("Warning: Division resulted in Infinity");
return Single.MaxValue;
}
return result;
}
public static void Main() {
Console.WriteLine("10 / 2 = " + Divide(10.0f, 2.0f));
Console.WriteLine("10 / 0 = " + Divide(10.0f, 0.0f));
Console.WriteLine("0 / 0 = " + Divide(0.0f, 0.0f));
}
}
The output of the above code is −
10 / 2 = 5 Warning: Division resulted in Infinity 10 / 0 = 3.402823E+38 Warning: Division resulted in NaN 0 / 0 = 0
Conclusion
The Single.IsNaN() method is essential for validating floating-point calculations in C#. It returns true only when a value is NaN (resulting from undefined operations like 0/0), and false for regular numbers and infinity values. Use this method to handle mathematical edge cases and ensure robust floating-point arithmetic.
