# Deal with NaN double value

In this chapter you will learn:

1. What is double type NaN(Not a Number)

### double NaN

Dividing `0.0` by `0.0` returns `NaN`. square root of a negative number is `NaN`. For example, `System.out.println(0.0/0.0)` and `System.out.println(Math.sqrt(-1.0))` output NaN.

Dividing a positive number by `+infinity` outputs `+0.0`. For example, `System.out.println(1.0/(1.0/0.0));` outputs `+0.0`.

Dividing a negative number by `+infinity` outputs `-0.0`. For example, `System.out.println(-1.0/(1.0/0.0));` outputs `-0.0`.

``````public class Main {
public static void main(String[] args) {
Double d1 = new Double(+0.0);
System.out.println(d1.doubleValue());
//ja v  a2  s  .c o m
Double d2 = new Double(-0.0);
System.out.println(d2.doubleValue());
System.out.println(d1.equals(d2));
System.out.println(+0.0 == -0.0);

}
}
``````

The following code parses command-line arguments into double precision floating-point values

``````public class Main {
public static void main(String[] args) {
if (args.length != 3) {
System.err.println("usage: java Main value1 op value2");
System.err.println("op is one of +, -, *, or /");
return;/*from   j  a v a 2s.  co m*/
}
try {
double value1 = Double.parseDouble(args[0]);
double value2 = Double.parseDouble(args[2]);
if (args[1].equals("+")) {
System.out.println(value1 + value2);
} else if (args[1].equals("-")) {
System.out.println(value1 - value2);
} else if (args[1].equals("*")) {
System.out.println(value1 * value2);
} else if (args[1].equals("/")) {
System.out.println(value1 / value2);
} else {
System.err.println("invalid operator: " + args[1]);
}
} catch (Exception nfe) {
System.err.println("Bad number format: " + nfe.getMessage());
}
}
}
``````

The `Double` class wraps a value of the primitive type double in an object. An object of type `Double` contains a single field whose type is double.

`Double` class provides several methods for converting a double to a String and a String to a double.

#### Next chapter...

What you will learn in the next chapter:

1. How to compare two double values
Home » Java Tutorial » Primitive Data Types