Open In App

NaN (Not a Number) in Java

Improve
Improve
Like Article
Like
Save
Share
Report

Can You guess the output of following code fragment:




public class Test
{
    public static void main(String[] args)
    {
        System.out.println(2 % 0);
    }
}       


Yes, You guessed it right: ArithmeticException
Output:

Exception in thread "main" java.lang.ArithmeticException: / by zero
	at traps.Test.main(Test.java:3)

Now guess the Output of :




public class Test
{
    public static void main(String[] args)
    {
        System.out.println(2.0 % 0);
    }
}       


Did you guessed it right ?
Output:

NaN


What is NaN?

NaN” stands for “not a number”. “Nan” is produced if a floating point operation has some input parameters that cause the operation to produce some undefined result. For example, 0.0 divided by 0.0 is arithmetically undefined. Finding out the square root of a negative number too is undefined.




//Java Program to illustrate NaN
public class Test
{
    public static void main(String[] args)
    {
        System.out.println(2.0 % 0);
        System.out.println(0.0 / 0);
        System.out.println(Math.sqrt(-1));
    }
}       


Output:

NaN
NaN
NaN

In javadoc, the constant field NaN is declared as following in the Float and Double Classes respectively.

public static final float 	NaN = 0f / 0f;
public static final double      NaN = 0d / 0d;

How to Compare NaN Values?

All numeric operations with NaN as an operand produce NaN as a result. Reason behind this is that NaN is unordered, so a numeric comparison operation involving one or two NaNs returns false.

  • The numerical comparison operators <, <=, >, and >= always return false if either or both operands are NaN.(§15.20.1)
  • The equality operator == returns false if either operand is NaN.
  • The inequality operator != returns true if either operand is NaN . (§15.21.1)




// Java program to test relational operator on NaN
public class ComparingNaN
{
    public static void main(String[] args)
    {
        // comparing NaN constant field defined in
        // Float Class
        System.out.print("Check if equal :");
        System.out.println(Float.NaN == Float.NaN);
          
        System.out.print("Check if UNequal: ");
        System.out.println(Float.NaN != Float.NaN);
  
        // comparing NaN constant field defined in Double Class
        System.out.print("Check if equal: ");
        System.out.println(Double.NaN == Double.NaN);
          
        System.out.print("Check if UNequal: ");
        System.out.println(Double.NaN != Double.NaN);
  
  
        // More Examples
        double NaN = 2.1 % 0;
        System.out.println((2.1%0) == NaN);
        System.out.println(NaN == NaN);
    }
}


Output:

Check if equal :false
Check if UNequal: true
Check if equal: false
Check if UNequal: true
false
false

isNaN() method

This method returns true if the value represented by this object is NaN; false otherwise.




   
import java.lang.*;
  
public class isNan
{
  
   public static void main(String[] args)
   {
  
     Double x = new Double(-2.0/0.0);
     Double y = new Double(0.0/0.0);
       
       
     // returns false if this Double value is not a Not-a-Number (NaN) 
     System.out.println(y + " = " + y.isNaN());
    
     // returns true if this Double value is a Not-a-Number (NaN) 
     System.out.println(x + " = " + x.isNaN());
    
   }


Output:

NaN = true
-Infinity = false

Floating type doesn’t produces Exception while operating with mathematical values

IEEE 754 floating point numbers can represent positive or negative infinity, and NaN (not a number). These three values arise from calculations whose result is undefined or cannot be represented accurately.
Java is following known math facts. 1.0 / 0.0 is infinity, but the others are indeterminate forms, which Java represents as NaN (not a number).




// Java program to illustrate output of floating
// point number operations
public class Test
{
    public static void main(String[] args)
    {
        System.out.println(2.0 / 0);
        System.out.println(-2.0 / 0);
        System.out.println(9.0E234 / 0.1E-234);
    }
}


Output:

Infinity
-Infinity
Infinity

References:
https://docs.oracle.com/javase/7/docs/api/java/lang/Double.html
https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html



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