Neden bu mantık
NaNanlamına gelir Not a Number. Sayı nedir? Herhangi bir şey. Bir tarafta ve diğer tarafta herhangi bir şey olabilir, bu yüzden hiçbir şey her ikisinin de eşit olduğunu garanti etmez. dokümantasyonunda görebileceğiniz ve aşağıdaki gibi NaNhesaplanır :Double.longBitsToDouble(0x7ff8000000000000L)longBitsToDouble
Bağımsız değişken aralığında bir değerde ise 0x7ff0000000000001Lyoluyla
0x7fffffffffffffffLya da aralığı 0xfff0000000000001Lthrough
0xffffffffffffffffLsonucu meydana gelir NaN.
Ayrıca, NaNAPI içinde mantıksal olarak ele alınır.
belgeleme
/**
* 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;
Bu arada, NaN bir kod örneği olarak test:
/**
* 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);
}
Çözüm
Ne yapabilirsiniz olan kullanım compare/ compareTo:
Double.NaNbu yöntemle kendisine eşit ve diğer tüm doubledeğerlerden (dahil
Double.POSITIVE_INFINITY) daha büyük olduğu düşünülür .
Double.compare(Double.NaN, Double.NaN);
Double.NaN.compareTo(Double.NaN);
Veya equals:
Eğer thisve argumentikisi de temsil Double.NaN, daha sonra equalsbu metot bize true, olsa bile
Double.NaN==Double.NaNdeğeri vardır false.
Double.NaN.equals(Double.NaN);