BigInteger 没有最大长度吗?如果它有我如何找到 BigInteger 数据类型的最大长度?
Does BigInteger not have a maximum length ? If it has how can I find the maximum length of BigInteger datatype?
我们可以通过内置MAX_VALUE找到Integer的最大长度。但是对于 BigInteger 它不起作用。
当我尝试对 BigInteger 使用 MAX_VALUE 时,它给出 >> 错误 >> 找不到符号变量 MAX_VALUE
public class ALL_datatype_length {
public static void main(String [] args){
byte a = Byte.MAX_VALUE;
short b = Short.MAX_VALUE;
int c = Integer.MAX_VALUE;
long d = Long.MAX_VALUE;
float e = Float.MAX_VALUE;
double f = Double.MAX_VALUE;
BigInteger g = BigInteger.MAX_VALUE; // >> Shows ERROR
System.out.println(a + "\t\t\t\tMaximum value of byte");
System.out.println(b + "\t\t\t\tMaximum value of short");
System.out.println(c + "\t\t\tMaximum value of integer");
System.out.println(d + "\t\tMaximum value of long");
System.out.println(e + "\t\t\tMaximum value of float");
System.out.println(f + "\t\tMaximum value of double");
}
}
假设您有内存,并且 JVM 将数组限制为 Integer.MAX_VALUE - 5
个元素(参见 Do Java arrays have a maximum size?),那么 BigInteger
的最大值有 mag
全部为 1 位的数组。
由于Integer.MAX_VALUE - 5
是2,147,483,642,每个数组元素是32位(int
),一个BigInteger
最多可以使用68,719,476,544位。
表示十进制数为20,686,623,727位。
那几乎是无限制的。肯定没有定义的最大值。
来自 Javadoc:Class BigInteger
BigInteger must support values in the range -2 Integer.MAX_VALUE (exclusive) to +2 Integer.MAX_VALUE (exclusive) and may support values outside of that range. An ArithmeticException is thrown when a BigInteger constructor or method would generate a value outside of the supported range. The range of probable prime values is limited and may be less than the full supported positive range of BigInteger. The range must be at least 1 to 2 500000000.
执行说明:
In the reference implementation, BigInteger constructors and operations throw ArithmeticException when the result is out of the supported range of -2 Integer.MAX_VALUE (exclusive) to +2 Integer.MAX_VALUE (exclusive).
不需要,但代码如下:
// Same can be done for the negative value as well
BigInteger twoBigInteger = BigInteger.valueOf(2);
BigInteger intMaxBigInteger = BigInteger.valueOf(Integer.MAX_VALUE);
BigInteger bigInteger = twoBigInteger.pow(intMaxBigInteger.intValue());
System.out.println(bigInteger.intValue());
异常:
Exception in thread "main" java.lang.ArithmeticException: BigInteger would overflow supported range
at java.math.BigInteger.reportOverflow(BigInteger.java:1084)
at java.math.BigInteger.checkRange(BigInteger.java:1079)
at java.math.BigInteger.<init>(BigInteger.java:1055)
at java.math.BigInteger.shiftLeft(BigInteger.java:3174)
at java.math.BigInteger.pow(BigInteger.java:2339)
at MyClass.main(MyClass.java:7)
我们可以通过内置MAX_VALUE找到Integer的最大长度。但是对于 BigInteger 它不起作用。
当我尝试对 BigInteger 使用 MAX_VALUE 时,它给出 >> 错误 >> 找不到符号变量 MAX_VALUE
public class ALL_datatype_length {
public static void main(String [] args){
byte a = Byte.MAX_VALUE;
short b = Short.MAX_VALUE;
int c = Integer.MAX_VALUE;
long d = Long.MAX_VALUE;
float e = Float.MAX_VALUE;
double f = Double.MAX_VALUE;
BigInteger g = BigInteger.MAX_VALUE; // >> Shows ERROR
System.out.println(a + "\t\t\t\tMaximum value of byte");
System.out.println(b + "\t\t\t\tMaximum value of short");
System.out.println(c + "\t\t\tMaximum value of integer");
System.out.println(d + "\t\tMaximum value of long");
System.out.println(e + "\t\t\tMaximum value of float");
System.out.println(f + "\t\tMaximum value of double");
}
}
假设您有内存,并且 JVM 将数组限制为 Integer.MAX_VALUE - 5
个元素(参见 Do Java arrays have a maximum size?),那么 BigInteger
的最大值有 mag
全部为 1 位的数组。
由于Integer.MAX_VALUE - 5
是2,147,483,642,每个数组元素是32位(int
),一个BigInteger
最多可以使用68,719,476,544位。
表示十进制数为20,686,623,727位。
那几乎是无限制的。肯定没有定义的最大值。
来自 Javadoc:Class BigInteger
BigInteger must support values in the range -2 Integer.MAX_VALUE (exclusive) to +2 Integer.MAX_VALUE (exclusive) and may support values outside of that range. An ArithmeticException is thrown when a BigInteger constructor or method would generate a value outside of the supported range. The range of probable prime values is limited and may be less than the full supported positive range of BigInteger. The range must be at least 1 to 2 500000000.
执行说明:
In the reference implementation, BigInteger constructors and operations throw ArithmeticException when the result is out of the supported range of -2 Integer.MAX_VALUE (exclusive) to +2 Integer.MAX_VALUE (exclusive).
不需要,但代码如下:
// Same can be done for the negative value as well
BigInteger twoBigInteger = BigInteger.valueOf(2);
BigInteger intMaxBigInteger = BigInteger.valueOf(Integer.MAX_VALUE);
BigInteger bigInteger = twoBigInteger.pow(intMaxBigInteger.intValue());
System.out.println(bigInteger.intValue());
异常:
Exception in thread "main" java.lang.ArithmeticException: BigInteger would overflow supported range
at java.math.BigInteger.reportOverflow(BigInteger.java:1084)
at java.math.BigInteger.checkRange(BigInteger.java:1079)
at java.math.BigInteger.<init>(BigInteger.java:1055)
at java.math.BigInteger.shiftLeft(BigInteger.java:3174)
at java.math.BigInteger.pow(BigInteger.java:2339)
at MyClass.main(MyClass.java:7)