I looked at the implementation of the java.lang.Double
class. The value of NaN
is the specified value of 0x7ff8000000000000L
. The public static final double NaN
field is set to 0.0d / 0.0
which should evaluate to 0x7ff8000000000000L
if the JVM does implement it that way.
Why was this value (
0x7ff8000000000000L
) chosen? Is there anything special about that value (e.g. its bit mask)?Why is the field implicitly set to that value and depends on the underlying implementation of the
0.0d / 0.0
operation whereas the static methodpublic static long doubleToLongBits(double value)
sets the value explicitly to0x7ff8000000000000L
for aNaN
argument? Wasn't it much safer to implicitly set it as the result of0.0d / 0.0
highly depends on the implementation of the JVM and could be changed (most likely it never will) theoretically?
The same goes for POSITIVE_INFINITY
and NEGATIVE_INFINITY
. Fields are implicitly set to their values but some methods use the explicit specified values. Is there a reason behind that?
Thanks for helping me to learn anything new each day :-).