为什么这样的逻辑
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);
}
解
您可以使用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.NaNfalse
Double.NaN.equals(Double.NaN);