Java,比较 BigInteger 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4095024/
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
Java, comparing BigInteger values
提问by Rosdi Kasim
BigInteger bigInteger = ...;
if(bigInteger.longValue() > 0) { //original code
//bigger than 0
}
//should I change to this?
if(bigInteger.compareTo(BigInteger.valueOf(0)) == 1) {
//bigger than 0
}
I need to compare some arbitary BigInteger values. I wonder which approach is correct. Given the above code which one should be used? The original code is on the top.. I am thinking to change it to the second approach.
我需要比较一些任意的 BigInteger 值。我想知道哪种方法是正确的。鉴于上面的代码应该使用哪一个?原来的代码在上面。。我想把它改成第二种方法。
采纳答案by Andreas Dolk
The first approach is wrong if you want to test if the BigInteger has a postive value: longValue
just returns the low-order 64 bitwhich may revert the sign... So the test could fail for a positive BigInteger.
如果您想测试 BigInteger 是否具有正值,则第一种方法是错误的:longValue
只返回低位 64 位,这可能会恢复符号......因此测试可能会因 BigInteger 为正而失败。
The second approach is better (see Bozhos answerfor an optimization).
Another alternative: BigInteger#signum
returns 1
if the value is positive:
另一种选择:如果值为正则BigInteger#signum
返回1
:
if (bigInteger.signum() == 1) {
// bigger than 0
}
回答by Bozho
If you are using BigInteger
, this assumes you need bigger numbers than long
can handle. So don't use longValue()
. Use compareTo
. With your example it better be:
如果您使用的是BigInteger
,则假定您需要的数字超出了long
处理能力。所以不要使用longValue()
. 使用compareTo
. 用你的例子最好是:
if (bigInteger.compareTo(BigInteger.ZERO) > 0) {
}
回答by Sean Patrick Floyd
This is not a direct answer, but an important note about using compareTo().
这不是直接的答案,而是关于使用 compareTo() 的重要说明。
When checking the value of compareTo(), always test for x < 0
, x > 0
and x == 0
.
Do not test for x == 1
检查 compareTo() 的值时,始终测试x < 0
,x > 0
和x == 0
。
不要测试x == 1
From the Comparable.compareTo()javadocs:
来自Comparable.compareTo()javadocs:
Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
将此对象与指定的对象进行比较以进行排序。当此对象小于、等于或大于指定对象时,返回一个负整数、零或正整数。
Note:
笔记:
A negative integer
, not-1
.A positive integer
, not1
.
A negative integer
,不是-1
。A positive integer
,不是1
。
True, checking for ==1
and ==-1
would work for BigInteger
. This is the BigInteger.compareTo()
code:
是的,检查==1
并==-1
适用于BigInteger
. 这是BigInteger.compareTo()
代码:
public int compareTo(BigInteger val) {
if (signum == val.signum) {
switch (signum) {
case 1:
return compareMagnitude(val);
case -1:
return val.compareMagnitude(this);
default:
return 0;
}
}
return signum > val.signum ? 1 : -1;
}
But it's still bad practice, and explicitly recommended against in the JavaDocs:
但这仍然是不好的做法,并且在JavaDocs 中明确建议反对:
Compares this BigInteger with the specified BigInteger. This method is provided in preference to individual methods for each of the six boolean comparison operators (<, ==, >, >=, !=, <=). The suggested idiom for performing these comparisons is: (
x.compareTo(y) <op> 0
), where<op>
is one of the six comparison operators.
将此 BigInteger 与指定的 BigInteger 进行比较。对于六个布尔比较运算符(<、==、>、>=、!=、<=)中的每一个,此方法优先于单独的方法提供。执行这些比较的建议习语是:(
x.compareTo(y) <op> 0
),其中<op>
是六个比较运算符之一。