Java 比较双精度值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33971989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Comparison double value
提问by Shiladittya Chakraborty
I have two double values. I want to check if two double values are same or not. I have two way to compare this double values.
我有两个双重值。我想检查两个双精度值是否相同。我有两种方法来比较这个双值。
First way :
第一种方式:
double a = 0.1;
double b = 0.2;
if(a == b) {
System.out.println("Double values are same");
}
Another way to compare :
另一种比较方式:
if(Double.compare(a, b) == 0) {
System.out.println("Double values are same");
}
Which one is the best way and accurate? Are both ways the same for comparison double values?
哪种方法最好且准确?比较双精度值的两种方式是否相同?
采纳答案by Tunaki
Double.compare
handles the case of NaN and negative zeros differently than ==
. For Double.compare
, -0.0d
is not equal to 0.0d
.
Double.compare
处理 NaN 和负零的情况与==
. 对于Double.compare
,-0.0d
不等于0.0d
。
For example the following code:
例如下面的代码:
public static void main(String... args) {
double a = -0.0d;
double b = 0.0d;
if (a == b) {
System.out.println("a == b");
}
if (Double.compare(a, b) == 0) {
System.out.println("Double.compare(a, b) == 0");
}
}
will only print a == b
.
只会打印a == b
.
The two operations are thus not equivalent. Using one over the other depend on your requirement.
因此这两个操作是不等价的。使用其中之一取决于您的要求。
The exact rule concerning ==
(and !=
) can be found in the JLS, section 15.21.1:
有关==
(和!=
)的确切规则可以在 JLS 的第 15.21.1 节中找到:
Floating-point equality testing is performed in accordance with the rules of the IEEE 754 standard:
If either operand is NaN, then the result of == is false but the result of != is true. Indeed, the test x!=x is true if and only if the value of x is NaN.
Positive zero and negative zero are considered equal.
Otherwise, two distinct floating-point values are considered unequal by the equality operators. In particular, there is one value representing positive infinity and one value representing negative infinity; each compares equal only to itself, and each compares unequal to all other values.
Subject to these considerations for floating-point numbers, the following rules then hold for integer operands or for floating-point operands other than NaN:
- The value produced by the == operator is true if the value of the left-hand operand is equal to the value of the right-hand operand; otherwise, the result is false.
- The value produced by the != operator is true if the value of the left-hand operand is not equal to the value of the right-hand operand; otherwise, the result is false.
浮点相等性测试按照 IEEE 754 标准的规则执行:
如果任一操作数为 NaN,则 == 的结果为假,但 != 的结果为真。实际上,当且仅当 x 的值为 NaN 时,测试 x!=x 才为真。
正零和负零被认为是相等的。
否则,相等运算符将两个不同的浮点值视为不相等。特别是,有一个值代表正无穷大,一个值代表负无穷大;每个比较只等于它自己,每个比较不等于所有其他值。
根据对浮点数的这些考虑,以下规则适用于整数操作数或 NaN 以外的浮点操作数:
- 如果左侧操作数的值等于右侧操作数的值,则 == 运算符产生的值为真;否则,结果为假。
- 如果左侧操作数的值不等于右侧操作数的值,则 != 运算符产生的值为真;否则,结果为假。
回答by Parris Varney
Both of those options are perfectly valid for checking the equality of two numbers. I personally would say the == is nicer, but that's just me.
这两个选项对于检查两个数字的相等性都是完全有效的。我个人会说 == 更好,但这只是我。
When double.compare() is better is when you want to know why your variables are not equal. It returns a little more information than true or false - a negative value if the left side is smaller, and a positive if the right side is smaller.
当 double.compare() 更好时,您想知道为什么您的变量不相等。它返回的信息比 true 或 false 多一点——如果左侧较小则为负值,如果右侧较小则为正值。
回答by skypHyman
Let's analyze this part:
我们来分析一下这部分:
if(Double.compare(a, b) == 0)
By looking at the documentationof Double.compare
, a possible implementation could be the following one (obviously it will be a more optimized one, but that's for the sake of the discussion):
通过查看文档的Double.compare
,一个可能的实现可能是以下一个(显然这将是一个更优化的一个,但对讨论的缘故):
return a == b ? 0 : (a < b ? -1 : +1);
Then, you have another comparison, so it becomes:
然后,你有另一个比较,所以它变成:
if((a == b ? 0 : (a < b ? -1 : +1)) == 0)
In the other case, you rely on a simple comparison using ==
, that is:
在另一种情况下,您依赖于使用 的简单比较==
,即:
if(a == b)
That said, in terms of accuracy I guess the result is the same, for the underlying representation of a double
does not change and the comparison with 0 does not seem to affect the accuracy.
也就是说,就准确性而言,我猜结果是一样的,因为 a 的底层表示double
没有改变,并且与 0 的比较似乎不会影响准确性。
Which is the best?
哪个最好?
Well, from the example above, I'd say the simpler one for you directly compare the values and you are interested only in equality, even though it's unlikely that you are facing with a problem which will benefit from choosing the best way for such a comparison.
Anyway, the approach using Double.compare
is more suitable for those cases when you are not only interested in equality, but also in the concepts of greater thanand/or less than.
好吧,从上面的例子来看,我会说更简单的方法是让你直接比较值,你只对平等感兴趣,即使你面临的问题不太可能从为这样的选择最好的方法中受益比较。无论如何,Double.compare
当您不仅对相等性感兴趣,而且对大于和/或小于的概念感兴趣时,使用的方法更适合于那些情况。