4

java.lang.Math#min(double, double)

public static double min(double a, double b) {
    if (a != a) return a; // a is NaN
    if (a == 0.0d && b == 0.0d && Double.doubleToLongBits(b) == negativeZeroDoubleBits) return b;
    return (a <= b) ? a : b;
}

在什么情况下可以a != a返回true?似乎a是NaN,但我无法想象一个例子。你能提供一个吗?

4

2 回答 2

10

一个简单的例子是

double d = Double.NaN; // or
double d = 0.0/0.0; // or
double d = Double.POSITIVE_INFINITY + Double.NEGATIVE_INFINITY;
if (Double.isNaN(a)) { // tests if a != a
   // do something

顺便说一句 Double.compare() 确实看到 NaN 相等

if (Double.compare(d, d) == 0) // always.

对于多个线程,这对于任何类型和值都是可能的。例如

if (a != /* another thread changes 'a' */ a) {
    // a thread changed a while you were looking at it.
于 2013-08-27T08:22:36.863 回答
0

如果 a 是 NAN,并且 NAN 例如被零除

min(0.0f/0.0f, 0.0f)

于 2013-08-27T08:24:27.653 回答