gpt4 book ai didi

java - 关系运算符在哪些方面不遵守浮点值的 compareTo 契约?

转载 作者:搜寻专家 更新时间:2023-10-30 21:27:38 30 4
gpt4 key购买 nike

引自 Effective Java - Second Edition by Joshua Bloch

For floating-point fields, use Double.compare or Float.compare in place of the relational operators, which do not obey the general contract for compareTo when applied to floating point values.

它没有详细说明为什么会这样。

那么,我的问题是:

当与浮点值一起使用时,关系运算符在哪些方面不遵守 compareTo 的一般约定?

最佳答案

来自javadoc :

 public int compareTo(Double anotherDouble)

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.

关于java - 关系运算符在哪些方面不遵守浮点值的 compareTo 契约?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12866922/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com