Java 大于 Long.MAX_VALUE 的 long
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16546038/
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
A long bigger than Long.MAX_VALUE
提问by gongshw
How can I get a long number bigger than Long.MAX_VALUE?
如何获得大于 Long.MAX_VALUE 的长数?
I want this method to return true
:
我希望这个方法返回true
:
boolean isBiggerThanMaxLong(long val) {
return (val > Long.MAX_VALUE);
}
采纳答案by djechlin
That method can't return true
. That's the pointof Long.MAX_VALUE
. It would be reallyconfusing if its name were... false. Then it should be just called Long.SOME_FAIRLY_LARGE_VALUE
and have literally zero reasonable uses. Just use Android's isUserAGoat
, or you may roll your own function that always returns false
.
该方法无法返回true
。这是该点的Long.MAX_VALUE
。如果它的名字是……假的,那真的会令人困惑。那么它应该只是被调用Long.SOME_FAIRLY_LARGE_VALUE
并且实际上是零合理使用。只需使用Android 的isUserAGoat
,或者您可以推出自己的始终返回false
.
Note that a long
in memory takes a fixed number of bytes. From Oracle:
请注意,long
内存中的 a 占用固定数量的字节。 从甲骨文:
long: The long data type is a 64-bit signed two's complement integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive). Use this data type when you need a range of values wider than those provided by int.
long:long 数据类型是一个 64 位有符号二进制补码整数。它的最小值为 -9,223,372,036,854,775,808,最大值为 9,223,372,036,854,775,807(含)。当您需要比 int 提供的值范围更广的值时,请使用此数据类型。
As you may know from basic computer science or discrete math, there are 2^64 possible values for a long, since it is 64 bits. And as you know from discrete math or number theory or common sense, if there's only finitely many possibilities, one of them has to be the largest. That would be Long.MAX_VALUE
. So you are asking something similar to "is there an integer that's >0 and < 1?" Mathematically nonsensical.
正如您从基础计算机科学或离散数学中知道的那样,long 有 2^64 个可能的值,因为它是 64 位。正如你从离散数学、数论或常识中知道的那样,如果只有有限多种可能性,那么其中之一必须是最大的。那将是Long.MAX_VALUE
。所以你问的问题类似于“是否有一个大于 0 且小于 1 的整数?” 数学上毫无意义。
If you actually need this for something for real then use BigInteger
class.
如果你真的需要这个来做一些真正的事情,那么使用BigInteger
类。
回答by James Cronen
You can't. If you have a method called isBiggerThanMaxLong(long)
it should always return false
.
你不能。如果你有一个方法被调用,isBiggerThanMaxLong(long)
它应该总是返回false
。
If you were to increment the bits of Long.MAX_VALUE
, the next value should be Long.MIN_VALUE
. Read up on twos-complement and that should tell you why.
如果要增加 的位Long.MAX_VALUE
,则下一个值应该是Long.MIN_VALUE
。阅读二进制补码,这应该会告诉您原因。
回答by NINCOMPOOP
Firstly, the below method doesn't compile as it is missing the return type and it should be Long.MAX_VALUE
in place of Long.Max_value
.
首先,下面的方法不能编译,因为它缺少返回类型,它应该Long.MAX_VALUE
代替Long.Max_value
.
public static boolean isBiggerThanMaxLong(long value) {
return value > Long.Max_value;
}
The above method can never return true
as you are comparing a long
value with Long.MAX_VALUE
, see the method signature you can pass only long
there.Any long
can be as big as the Long.MAX_VALUE
, it can't be bigger than that.
true
当您将long
值与比较时,上述方法永远不会返回 Long.MAX_VALUE
,请参阅您只能在long
那里传递的方法签名。任何long
可以与 一样大Long.MAX_VALUE
,但不能大于 。
You can try something like this with BigIntegerclass :
你可以用BigInteger类尝试这样的事情:
public static boolean isBiggerThanMaxLong(BigInteger l){
return l.compareTo(BigInteger.valueOf(Long.MAX_VALUE))==1?true:false;
}
The below code will return true
:
以下代码将返回true
:
BigInteger big3 = BigInteger.valueOf(Long.MAX_VALUE).
add(BigInteger.valueOf(Long.MAX_VALUE));
System.out.println(isBiggerThanMaxLong(big3)); // prints true
回答by Patricia Shanahan
If triangle.lborderA
is indeed a long then the test in the original code is trivially true, and there is no way to test it. It is also useless.
如果triangle.lborderA
确实很长那么原始代码中的测试是微不足道的,并且没有办法测试它。它也是无用的。
However, if triangle.lborderA
is a double, the comparison is useful and can be tested. isBiggerThanMaxLong(1e300)
does return true.
但是,如果triangle.lborderA
是双精度型,则比较是有用的并且可以进行测试。isBiggerThanMaxLong(1e300)
确实返回true。
public static boolean isBiggerThanMaxLong(double in){
return in > Long.MAX_VALUE;
}