是否有一个到BigInteger的上限?(复制)

时间:2021-06-30 16:47:50

Possible Duplicate:
What does BigInteger having no limit mean?

可能重复:无极限的BigInteger是什么意思?

The Javadoc for BigInteger does not define any maximum or minimum. However, it does say:

BigInteger的Javadoc不定义任何最大值或最小值。然而,它说:

(emphasis added)

(重点)

Immutable arbitrary-precision integers

不可变的任意精度的整数

Is there such a maximum, even in theory? Or is the way BigInteger operates fundamentally different, such that there is in reality no maximum except for the amount of memory available on the computer?

即使在理论上,是否存在这样的最大值?或者是BigInteger的操作方式有根本的不同,比如除了计算机上可用的内存之外,实际上没有最大值?

3 个解决方案

#1


54  

The number is held in an int[] - the maximum size of an array is Integer.MAX_VALUE. So the maximum BigInteger probably is (2 ^ 32) ^ Integer.MAX_VALUE.

这个数字是在int[]中保存的,数组的最大大小是Integer.MAX_VALUE。先导入BigInteger可能最大是(2 ^ 32)^ Integer.MAX_VALUE。

Admittedly, this is implementation dependent, not part of the specification.

诚然,这是依赖于实现的,而不是规范的一部分。


In Java 8, some information was added to the BigInteger javadoc, giving a minimum supported range and the actual limit of the current implementation:

在Java 8中,一些信息被添加到BigInteger javadoc中,给出了最小支持范围和当前实现的实际限制:

BigInteger must support values in the range -2Integer.MAX_VALUE (exclusive) to +2Integer.MAX_VALUE (exclusive) and may support values outside of that range.

BigInteger必须支持范围为-2Integer的值。MAX_VALUE(独家)+ 2整数。MAX_VALUE(独占),并可能支持该范围之外的值。

Implementation note: BigInteger constructors and operations throw ArithmeticException when the result is out of the supported range of -2Integer.MAX_VALUE (exclusive) to +2Integer.MAX_VALUE (exclusive).

实现说明:当结果超出了-2Integer的支持范围时,BigInteger构造函数和操作将抛出算术异常。MAX_VALUE(独家)+ 2整数。MAX_VALUE(独家)。

#2


17  

BigInteger would only be used if you know it will not be a decimal and there is a possibility of the long data type not being large enough. BigInteger has no cap on its max size (as large as the RAM on the computer can hold).

BigInteger只会被使用,如果你知道它不会是一个小数,并且有可能是长数据类型不够大。BigInteger没有其最大大小的上限(与计算机上的RAM所能容纳的大小一样大)。

From here.

从这里。

It is implemented using an int[]:

使用int[]实现:

  110       /**
  111        * The magnitude of this BigInteger, in <i>big-endian</i> order: the
  112        * zeroth element of this array is the most-significant int of the
  113        * magnitude.  The magnitude must be "minimal" in that the most-significant
  114        * int ({@code mag[0]}) must be non-zero.  This is necessary to
  115        * ensure that there is exactly one representation for each BigInteger
  116        * value.  Note that this implies that the BigInteger zero has a
  117        * zero-length mag array.
  118        */
  119       final int[] mag;

From the source

从源

From the Wikipedia article Arbitrary-precision arithmetic:

从*的文章中可以看出任意精确的算术:

Several modern programming languages have built-in support for bignums, and others have libraries available for arbitrary-precision integer and floating-point math. Rather than store values as a fixed number of binary bits related to the size of the processor register, these implementations typically use variable-length arrays of digits.

一些现代编程语言内置了对bignums的支持,还有一些语言有用于任意精度整数和浮点数的库。与其将值存储为与处理器寄存器大小相关的固定数量的二进制位,这些实现通常使用可变长度的数字数组。

#3


10  

The first maximum you would hit is the length of a String which is 231-1 digits. It's much smaller than the maximum of a BigInteger but IMHO it loses much of its value if it can't be printed.

第一个最大值是一个字符串的长度,它是231-1位。它比BigInteger的最大值小得多,但如果不能打印,它就失去了很多价值。

#1


54  

The number is held in an int[] - the maximum size of an array is Integer.MAX_VALUE. So the maximum BigInteger probably is (2 ^ 32) ^ Integer.MAX_VALUE.

这个数字是在int[]中保存的,数组的最大大小是Integer.MAX_VALUE。先导入BigInteger可能最大是(2 ^ 32)^ Integer.MAX_VALUE。

Admittedly, this is implementation dependent, not part of the specification.

诚然,这是依赖于实现的,而不是规范的一部分。


In Java 8, some information was added to the BigInteger javadoc, giving a minimum supported range and the actual limit of the current implementation:

在Java 8中,一些信息被添加到BigInteger javadoc中,给出了最小支持范围和当前实现的实际限制:

BigInteger must support values in the range -2Integer.MAX_VALUE (exclusive) to +2Integer.MAX_VALUE (exclusive) and may support values outside of that range.

BigInteger必须支持范围为-2Integer的值。MAX_VALUE(独家)+ 2整数。MAX_VALUE(独占),并可能支持该范围之外的值。

Implementation note: BigInteger constructors and operations throw ArithmeticException when the result is out of the supported range of -2Integer.MAX_VALUE (exclusive) to +2Integer.MAX_VALUE (exclusive).

实现说明:当结果超出了-2Integer的支持范围时,BigInteger构造函数和操作将抛出算术异常。MAX_VALUE(独家)+ 2整数。MAX_VALUE(独家)。

#2


17  

BigInteger would only be used if you know it will not be a decimal and there is a possibility of the long data type not being large enough. BigInteger has no cap on its max size (as large as the RAM on the computer can hold).

BigInteger只会被使用,如果你知道它不会是一个小数,并且有可能是长数据类型不够大。BigInteger没有其最大大小的上限(与计算机上的RAM所能容纳的大小一样大)。

From here.

从这里。

It is implemented using an int[]:

使用int[]实现:

  110       /**
  111        * The magnitude of this BigInteger, in <i>big-endian</i> order: the
  112        * zeroth element of this array is the most-significant int of the
  113        * magnitude.  The magnitude must be "minimal" in that the most-significant
  114        * int ({@code mag[0]}) must be non-zero.  This is necessary to
  115        * ensure that there is exactly one representation for each BigInteger
  116        * value.  Note that this implies that the BigInteger zero has a
  117        * zero-length mag array.
  118        */
  119       final int[] mag;

From the source

从源

From the Wikipedia article Arbitrary-precision arithmetic:

从*的文章中可以看出任意精确的算术:

Several modern programming languages have built-in support for bignums, and others have libraries available for arbitrary-precision integer and floating-point math. Rather than store values as a fixed number of binary bits related to the size of the processor register, these implementations typically use variable-length arrays of digits.

一些现代编程语言内置了对bignums的支持,还有一些语言有用于任意精度整数和浮点数的库。与其将值存储为与处理器寄存器大小相关的固定数量的二进制位,这些实现通常使用可变长度的数字数组。

#3


10  

The first maximum you would hit is the length of a String which is 231-1 digits. It's much smaller than the maximum of a BigInteger but IMHO it loses much of its value if it can't be printed.

第一个最大值是一个字符串的长度,它是231-1位。它比BigInteger的最大值小得多,但如果不能打印,它就失去了很多价值。