You are probably surprised that this special value could have been stored in a double
variable. This is because Java uses the IEEE 754 standard for floating point representation.
This standard allows to represent not just specific rational numbers, but also a few special values, like plus infinity or minus infinity, which are essentially very big numbers - results of calculations that yielded bigger results than any representable rational value. (There are also denormal numbers, lying somewhere between the finite and infinite values; precision of those is reduced compared to the precision you are used to, but not so extremely as with the infinities. The loss of precision is caused by giving more room to the exponent at the expense of the mantissa.)
However, a NaN
is not an infinity, nor an indicator of an overflow or an underflow. It is not an inaccurate result of any computation. It is a non-value, similar to a NULL
in the database. In fact, try this in your program:
System.out.println(d == d)
It will print out false
.
Why false? Loosely speaking, if you do not know the values of two quantities, there is no reason to assume that they are the same value. They usually are not.
It is actually quite unusual to see a NaN
on input to double.ParseDouble
, except during some kind of unmarshalling.