왜 그 논리
NaN의미 Not a Number합니다. 숫자가 아닌 것은 무엇입니까? 아무것도. 한쪽과 다른쪽에 무엇이든 가질 수 있으므로 둘 다 동일하다는 보장은 없습니다. NaN로 계산 Double.longBitsToDouble(0x7ff8000000000000L)되고 다음 문서에서 볼 수 있습니다 longBitsToDouble:
인수의 범위에있는 값이면 0x7ff0000000000001L통해
0x7fffffffffffffffL또는 범위의 0xfff0000000000001L관통은
0xffffffffffffffffL, 그 결과는이다 NaN.
또한 NaN논리적으로 API 내부에서 처리됩니다.
선적 서류 비치
/**
* A constant holding a Not-a-Number (NaN) value of type
* {@code double}. It is equivalent to the value returned by
* {@code Double.longBitsToDouble(0x7ff8000000000000L)}.
*/
public static final double NaN = 0.0d / 0.0;
그런데, NaN 되는 코드 샘플로 테스트 :
/**
* Returns {@code true} if the specified number is a
* Not-a-Number (NaN) value, {@code false} otherwise.
*
* @param v the value to be tested.
* @return {@code true} if the value of the argument is NaN;
* {@code false} otherwise.
*/
static public boolean isNaN(double v) {
return (v != v);
}
해결책
당신이 할 수있는 일은 use compare/ compareTo:
Double.NaN이 방법으로이 값은 자체와 같고 다른 모든 double값 (포함
Double.POSITIVE_INFINITY) 보다 큽니다 .
Double.compare(Double.NaN, Double.NaN);
Double.NaN.compareTo(Double.NaN);
또는 equals:
경우 this와 argument두 대표 Double.NaN, 다음 equals메소드가 리턴은 true, 비록
Double.NaN==Double.NaN값이 있습니다 false.
Double.NaN.equals(Double.NaN);