Open In App

NaN (Not a Number) in Java

Last Updated : 18 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Java, the meaning of NaN is Not a Number, and it is present in java.lang.Double and java.lang.Float classes. It is a special value that represents an undefined numeric value. We may encounter NAN in mathematical calculations where the result is undefined.

  • If we try to calculate the square root of a negative, Math.sqrt(-1), it will return NaN because the square root of a negative number is undefined.
  • If we try to divide zero by zero, it will also return NaN.

Example:

Java
// Java Program to demonstrates NAN

// Define a class
public class Geeks
{
    // main method
    public static void main(String[] args)
    {
        System.out.println(2.0 % 0.0);
        System.out.println(0.0 / 0.0);
        System.out.println(Math.sqrt(-1));
    }
}

Output
NaN
NaN
NaN

Sometimes maths does not make sense, that's why NaN exists. Instead of throwing errors, Java simply returns NaN to show that something is wrong.

How to Create NaN?

With the help of Double or Float classes we can create a NAN.

double n = Double.NaN; // Using Double

float n = Float.NaN; // Using Float

How to Compare NaN Values?

NaN are unordered, it means that any comparison with NaN will return false. If we compare NaN to another NaN or we are using relational operators, the result will always be false. Now, we are going to discuss how NaN works in different scenerios.

1. Relational Operators and NaN

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

Example:

Java
// Java program to test relational operator on NaN
public class Geeks
{
	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


2. isNaN() Method

This method is used to check is a value is NaN.

Example:

Java
// Demonstrating isNaN()

import java.lang.*;

// Created a class
public class Geeks
{
   // main method
   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


3. Floating Type Doesn’t Produces Exception While Operating with Mathematical Values

NaN follows the IEEE 754 floating-point standard. IEEE 754 floating point numbers can represent positive or negative infinity, and NaN. 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).

Example:

Java
// Demonstrating output of floating
// point number operations
public class Geeks
{
    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

Article Tags :
Practice Tags :

Similar Reads