Warum diese Logik?
NaNbedeutet Not a Number. Was ist keine Nummer? Etwas. Sie können alles auf der einen Seite und alles auf der anderen Seite haben, also garantiert nichts, dass beide gleich sind. NaNwird berechnet mit Double.longBitsToDouble(0x7ff8000000000000L)und wie Sie in der Dokumentation von sehen können longBitsToDouble:
Wenn das Argument ein Wert im Bereich 0x7ff0000000000001Ldurch
0x7fffffffffffffffLoder im Bereich 0xfff0000000000001Ldurch ist
0xffffffffffffffffL, ist das Ergebnis a NaN.
Wird auch NaNinnerhalb der API logisch behandelt.
Dokumentation
/**
* 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;
By the way, NaN wird als Ihr Codebeispiel getestet:
/**
* 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);
}
Lösung
Was Sie tun können, ist compare/ compareTo:
Double.NaNwird von dieser Methode als gleich und größer als alle anderen doubleWerte (einschließlich
Double.POSITIVE_INFINITY) angesehen.
Double.compare(Double.NaN, Double.NaN);
Double.NaN.compareTo(Double.NaN);
Oder equals:
Wenn thisund argumentbeide darstellen Double.NaN, gibt die equalsMethode zurück true, obwohl
Double.NaN==Double.NaNsie den Wert hat false.
Double.NaN.equals(Double.NaN);