BigInteger.valueOf() 限制

发布于 2024-08-18 03:48:49 字数 71 浏览 2 评论 0原文

BigInteger 的 valueOf 有任何限制吗?我不确定,但在某处读过,给定的数字只能是 length = long 。

Does valueOf for BigInteger have any limitations ? I'm not sure but read somewhere, that given number can be of length = long only.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

最冷一天 2024-08-25 03:48:49

BigInteger 类本身用于表示不可变的任意精度整数。这意味着它可以表示任何大小的整数(当然受计算机内存的限制)。

然而,valueOf 方法返回一个 BigInteger,其值等于指定的 long 值。因此,根据定义以这种方式创建的 BigInteger 只能与

其他 BigInteger 的方法和构造函数 > class 当然可以大于 Long.MAX_VALUE

以下面截取的代码为例:

BigInteger big1 = BigInteger.valueOf(Long.MAX_VALUE);
BigInteger big2 = BigInteger.valueOf(Long.MAX_VALUE);
BigInteger big3 = big1.add(big2);

名为 big3BigInteger 大于 Long.MAX_VALUE,即使其组成部分是使用 创建的>valueOf 方法。

The BigInteger class itself is used to represent immutable arbitrary-precision integers. Meaning it can represent integers of any size (limited of course by the memory on your computer).

However the valueOf method returns a BigInteger whose value is equal to that of the specified long. So a BigInteger created in this way by definition can only be a large as Long.MAX_VALUE

BigInteger objects created by the other methods and constructors of the BigInteger class can of course be larger than Long.MAX_VALUE.

Take for example the code snipped below:

BigInteger big1 = BigInteger.valueOf(Long.MAX_VALUE);
BigInteger big2 = BigInteger.valueOf(Long.MAX_VALUE);
BigInteger big3 = big1.add(big2);

The BigInteger named big3 is larger than Long.MAX_VALUE even though its constituent parts were created using the valueOf method.

寂寞陪衬 2024-08-25 03:48:49

BigIntegervalueOf() 方法将 long 作为其唯一参数。因此,您可以传递给它的最大数字是 long 可以表示的最大值 (2^63-1 = 9223372036854775807)。

BigInteger's valueOf() method thakes a long as its sole parameter. So the maximum number you can pass to it is the maximum a long can represent (2^63-1 = 9223372036854775807).

风情万种。 2024-08-25 03:48:49

According to The Java API Specification for the BigInteger class, the BigInteger.valueOf method takes a long as the argument, so the largest number that can be obtained through the BigInteger.valueOf method is Long.MAX_VALUE which is 2^63 - 1.

疯了 2024-08-25 03:48:49

考虑使用 BigInteger(String val, int radix) 构造函数。这可以创建任意大小的BigInteger

Consider using the BigInteger(String val, int radix) constructor. That can create a BigInteger of any size.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文