In Java, what happens if you use Double.NaN in an operation?

I compiled code that mistakenly tries to add a number and Double.NaN. I am wondering if he throws an exception that misses? Does anyone know how to handle this situation? Thanks.

+6
java nan
source share
3 answers

Adding a number to NaN gives NaN. This is not expected to throw an exception. I understand that this complies with IEEE 754.

+6
source share

To answer Steve B's question:

POSITIVE_INFINITY is the largest number you can save if you have unlimited storage space. Without this luxury, we must use a type 1.0 / 0.0 design that does an excellent job. The same goes for NEGATIVE_INFINITY, but then the largest negative number.

NaN is usually defined as 0.0 / 0.0 because there is no number like 0/0, which is ideal for NaN.

+1
source share
public static void main(String args[]) { Double d = Double.NaN + 1.0; System.out.println(d); } 

Prints Double.Nan. Can someone explain the original implementation?

  public static final double POSITIVE_INFINITY = 1.0 / 0.0; public static final double NEGATIVE_INFINITY = -1.0 / 0.0; public static final double NaN = 0.0d / 0.0; 
0
source share

All Articles