为什么Java的Double.compare(double,double)是这样实现的?
2022-09-01 03:35:44
						我正在研究Java标准库中比较(double,double)的实现(6)。上面写着:
public static int compare(double d1, double d2) {
    if (d1 < d2)
        return -1;       // Neither val is NaN, thisVal is smaller
    if (d1 > d2)
        return 1;        // Neither val is NaN, thisVal is larger
    long thisBits = Double.doubleToLongBits(d1);
    long anotherBits = Double.doubleToLongBits(d2);
    return (thisBits == anotherBits ?  0 : // Values are equal
            (thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
             1));                          // (0.0, -0.0) or (NaN, !NaN)
}
此实现的优点是什么?
编辑:“优点”是一个(非常)糟糕的词语选择。我想知道这是怎么回事。