android double比较大小吗,java – Android – 比较方法违反了它的一般...

没有用于重新发明轮子.我相信你应该只返回lhs.getDist().compareTo(rhs.getDist());让提供的实现compareTo完成这项工作.

Compares two Double objects numerically.

There are two ways in which comparisons performed by this method differ from those performed by the Java language numerical comparison operators (=, >) when applied to primitive double values:

Double.NaN is considered by this method to be equal to itself and greater than all other double values (including Double.POSITIVE_INFINITY).

0.0d is considered by this method to be greater than -0.0d.

This ensures that the natural ordering of Double objects imposed by this method is consistent with equals.

我相信你会得到这个例外,因为你现在的实现可能不适合处理Double.NaN和正/负零值,但仍然遵守一般合同.查看OpenJDK Double#compare(double,double)源代码:

public static int More ...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)

}

Note that in most cases, for two instances of class Double, d1 and d2, the value of d1.equals(d2) is true if and only if

d1.doubleValue() == d2.doubleValue()

also has the value true. However, there are two exceptions:

If d1 and d2 both represent Double.NaN, then the equals method returns true, even though Double.NaN==Double.NaN has the value false.

If d1 represents +0.0 while d2 represents -0.0, or vice versa, the equal test has the value false, even though +0.0==-0.0 has the value true.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值