Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

The documentation for java.lang.Double.NaN says that it is

A constant holding a Not-a-Number (NaN) value of type double. It is equivalent to the value returned by Double.longBitsToDouble(0x7ff8000000000000L).

This seems to imply there are others. If so, how do I get hold of them, and can this be done portably?

To be clear, I would like to find the double values x such that

Double.doubleToRawLongBits(x) != Double.doubleToRawLongBits(Double.NaN)

and

Double.isNaN(x)

are both true.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
124 views
Welcome To Ask or Share your Answers For Others

1 Answer

You need doubleToRawLongBits rather than doubleToLongBits.

doubleToRawLongBits extracts the actual binary representation. doubleToLongBits doesn't, it converts all NaNs to the default NaN first.

double n = Double.longBitsToDouble(0x7ff8000000000000L); // default NaN
double n2 = Double.longBitsToDouble(0x7ff8000000000100L); // also a NaN, but M != 0

System.out.printf("%X
", Double.doubleToLongBits(n));
System.out.printf("%X
", Double.doubleToRawLongBits(n));
System.out.printf("%X
", Double.doubleToLongBits(n2));
System.out.printf("%X
", Double.doubleToRawLongBits(n2));

output:

7FF8000000000000
7FF8000000000000
7FF8000000000000
7FF8000000000100

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...