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

Categories

0 votes
126 views
in Technique[技术] by (71.8m points)

java - What are the other NaN values?

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

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

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...