Java

NaN in Java

In Java, there can be situations where there is a need to refrain from the β€œNaN” values. More specifically, while dealing with garbage values or mathematical computations accumulating unnecessary memory. In such cases, β€œNaN” in Java is of great aid in checking for a valid number and managing the memory effectively.

This article will demonstrate the usage and occurrence of β€œNaN” in Java.

What is β€œNaN” in Java?

β€œNaN(Not a Number)” in Java is a special floating point value referring to errors. It usually corresponds to the outcome of invalid operations. For example, dividing zero by zero, dividing a floating point number by zero, or taking the square root of a negative number, etc.

Example 1: Operations Leading to β€œNaN” in Java

In this example, the computations resulting in the β€œNaN” outcome can be implemented:

public class nan {
 public static void main(String[] args) {
  System.out.println("Zero by Zero: "+0.0 / 0.0);  
  System.out.println("Mod by Zero: "+10.0 % 0);
  System.out.println("Square Root of -1: " + Math.sqrt(-1));
  System.out.println("Log of -1: " + Math.log(-1));
}}

 
In the above lines of code, divide zero by zero, take β€œmod” by zero, and return the β€œsquare root” and β€œlog” of a negative number, respectively. All of these computations lead to a β€œNaN” outcome.

Output


In the above output, it can be seen that all of the calculations yield β€œNaN”, as a result.

Example 2: Comparing β€œNaN” Values in Java

In this particular example, the β€œNaN” values corresponding to various classes can be compared. Before proceeding to the example, consider the following points:

    • The comparison operators β€œ<”, β€œ<=”, ”>”, and β€œ>=” always return β€œfalse” if either or both the operands are β€œNaN”.
    • The equality operator β€œ==” returns β€œfalse” if either of the operands is NaN.
    • The inequality operator β€œ!=” returns β€œtrue” if either of the operands is NaN .

Now, let’s move on to the following lines of code:

public class nan2 {
 public static void main(String[] args) {
  System.out.println (Float.NaN != Float.NaN);
  System.out.println (Float.NaN == Float.NaN);
  System.out.println (Float.NaN < Float.NaN);
  System.out.println (Float.NaN <= Float.NaN);
  System.out.println (Float.NaN >= Float.NaN);
  System.out.println (Double.NaN != Double.NaN);
  System.out.println (Double.NaN == Double.NaN);
  System.out.println (Double.NaN > Double.NaN);
  System.out.println (Double.NaN <= Double.NaN);
  System.out.println (Double.NaN >= Double.NaN);
}}

 
In this code snippet, specify β€œFloat.NaN” and β€œDouble.NaN” for the constant fields in both classes, respectively to perform the comparisons. Note that the comparison is done based on the discussed specified conditions and the corresponding boolean outcome is returned.

Output


In this outcome, it can be analyzed that only the comparison against the β€œ!=” operator returned the outcome β€œtrue” in both classes accordingly.

Conclusion

β€œNan(Not a Number)” in Java usually corresponds to the outcome of invalid operations like dividing zero by zero, dividing a floating point number by zero, or taking the square root of a negative number, etc. This blog discussed the usage and implementation of β€œNaN” in Java.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.