java中double..compare_为什么Java的Double.compare(double,double)实现了它的样子?

我在Java标准库(6)中查看了

compare(double, double)的实现。内容如下:

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)

}

这种实现的优点是什么?

编辑:“优点”是一个(非常)坏的词选择。我想知道这是如何工作的。


版权声明:本文为weixin_33700809原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。