Open In App

Double.IsNaN() Method in C#

Improve
Improve
Like Article
Like
Save
Share
Report

In C#, Double.IsNaN() is a Double struct method. This method is used to check whether the specified value is not a number (NaN).

Syntax: public static bool IsNaN (double d);

Parameter:
d: It is a double-precision floating-point number of type System.Double

Return Type: This function returns a Boolean value i.e. True, if specified value is not a number(NaN), otherwise returns False.

Example:

Input  : d = 0.0 / 0.0 
Output : True

Input  : d = 1.734
Output : False

Code: To demonstrate the Double.IsNaN(Double) method.




// C# code to demonstrate 
// Double.IsNaN(Double) method 
using System;
  
class GFG { 
      
    // Main Method
    public static void Main(String[] args) 
    
  
        // first example 
        Double f1 = 1.0 / 0.0; 
  
        bool res = Double.IsNaN(f1); 
  
        // printing the output 
        if (res) 
            Console.WriteLine(f1 + " is NaN"); 
        else
            Console.WriteLine(f1 + " is not NaN"); 
  
        // second example 
        double f2 = 0.0 / 0.0; 
  
        bool res1 = Double.IsNaN(f2); 
  
        // printing the output 
        if (res1) 
            Console.WriteLine(f2 + " is NaN"); 
        else
            Console.WriteLine(f2 + " is not NaN"); 
    


Output:

Infinity is not NaN
NaN is NaN

Note:

  • If we divide any value by 0 directly, the Compiler will show an error. So, in the above example, 0 is stored in a variable first.
  • Every floating point operation returns a NaN to show that the result of the operation is undefined.

Last Updated : 13 Feb, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads