なぜそのロジック
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.NaN
false
Double.NaN.equals(Double.NaN);