Javatpoint Logo
Javatpoint Logo

Double.IsNaN() Method in C#

The double.IsNaN() method in C# is a part of the "System" namespace, which is used to check whether the given double-precision floating point value is a "Not-a-Number" (NaN) value.

NaN is a special value defined by the IEE 754 floating point standard. NaN is a special value that represents the result of an undefined or unrepresentable mathematical operation, such as dividing zero by zero. NaN is often used to signal errors or exceptional conditions in floating-point computations.

Purpose of Double.IsNaN() method

This method determines whether a given double-precision floating point value is equal to NaN. It returns the Boolean value "true" if the input is NaN; if the given value is not NaN, it will return "false".

Syntax:

It has the following syntax:

This method will take one parameter. Here, it is denoted by the d.

The return type of the method is Boolean. It returns either true or false

Example:

Let us take a C# program to illustrate the Double.IsNaN() method.

Output:

Double.IsNaN() Method in C#

Explanation:

This program is used to demonstrate the usage of the IsNaN() method in C#. In the main method, there are two examples that are assigned to two variables: one is result1, and the other variable is validValue. Here, the result, which is obtained after applying the division by zero, resulting in NaN, is given to the result1 variable. A double value is assigned to a valid value variable that is 100. After that, the CheckAndPrintResult function says whether the given number is a valid double floating number or a NaN. The result1 variable is given to Double.IsNaN method, which gives true for the result1 variable and other variable, is classified as Not a Number.

Usages of the IsNaN() method

1. Checking for NaN

2. For validating input

Output:

Double.IsNaN() Method in C#

Explanation:

The C# program prompts the user to enter a numeric value, attempts to parse the input as a double using the TryParseDouble method, and checks for NaN using Double.IsNaN(). If the input is valid, it acknowledges it; otherwise, it reports an error and prompts the user to enter a valid numeric value. The TryParseDouble method encapsulates the parsing logic, ensuring that NaN values are handled appropriately, and the program provides informative messages based on the user input, guiding the user towards correct numeric entries.

3. Handling NaN in Calculations

Output:

Double.IsNaN() Method in C#

Explanation:

This C# program solicits two numeric values for division from the user, employing the TryParseDouble method to ensure valid numeric input. After that, it checks whether the denominator is zero to avoid division by zero errors. If the denominator is nonzero, the program proceeds with the division, checking for NaN using Double.IsNaN(). Depending on the result, it displays the division outcome or issues an error message if NaN is encountered. The program offers clear and informative feedback, guiding the user through correct input procedures, and its structured approach ensures proper handling of potential issues, such as division by zero or non-numeric entries.

Conclusion:

NaN can propagate through calculations, potentially leading to unexpected results. Double.IsNaN() is generally efficient, but excessive use in performance-critical code might warrant alternatives.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA