比Long.MAX_VALUE大很多 [英] A long bigger than Long.MAX_VALUE

查看:87
本文介绍了比Long.MAX_VALUE大很多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获得比Long.MAX_VALUE大的整数?

How can I get a long number bigger than Long.MAX_VALUE?

我希望此方法返回true:

boolean isBiggerThanMaxLong(long val) {
    return (val > Long.MAX_VALUE);
}

推荐答案

该方法无法返回true.这就是Long.MAX_VALUE要点.如果它的名字是……假,那将是真的令人困惑.然后,应将其称为Long.SOME_FAIRLY_LARGE_VALUE,并且合理地使用零个字.只需使用 Android的isUserAGoat ,否则您可以滚动自己的函数,该函数始终返回false.

That method can't return true. That's the point of Long.MAX_VALUE. It would be really confusing 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.

请注意,内存中的long占用固定数量的字节. 来自Oracle :

Note that a long in memory takes a fixed number of bytes. From Oracle:

long:long数据类型是64位带符号的二进制补码整数.最小值为-9,223,372,036,854,775,808,最大值为9,223,372,036,854,775,807(含).当您需要的值范围比int提供的值宽时,请使用此数据类型.

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.

您可能会从基础计算机科学或离散数学中学到的,因为它是64位,所以很长的时间内可能有2 ^ 64个值.从离散数学,数论或常识中可以知道,如果只有有限的多种可能性,则其中之一必须是最大的.那将是Long.MAX_VALUE.因此,您在问类似是否有一个大于0且小于1的整数?"的问题.数学上没有意义.

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.

如果您实际需要此功能,请使用BigInteger类.

If you actually need this for something for real then use BigInteger class.

这篇关于比Long.MAX_VALUE大很多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆