问题描述
我在程序中将两个非常大的BigInteger值相乘.失败了BigInteger
和BigDecimal
的限制是什么?
I was multiplying very two huge BigIntegervalues in a program. It failed. What are the limits of BigInteger
and BigDecimal
?
推荐答案
您不会获得NumberFormatException与大数字相乘的结果.如果生成的数字太大,则在数组大小溢出时,您将得到一个神秘的NegativeArraySizeException.
You won't get NumberFormatException multiplying large numbers. If the number produced is too large, you will get a cryptic NegativeArraySizeException as the size of the array overflows.
您更有可能遇到内存不足错误.
You are more likely to get an out of memory error.
BigInteger的限制为32 * 2 ^ 32-1位,或大约2 ^(40亿).
The limit is 32 * 2^32-1 bits for BigInteger or about 2^(4 billion).
如果您可以获取NumberFormatException
You can get a NumberFormatException if you
- 从一个空字节创建一个BigInteger []
- 使用符号< -1或> +1
- 尝试解析以> 36或<为底的数字2
- 有一个包含非法数字的字符串.
当您遇到异常时,您还应该查看消息和堆栈跟踪,因为这通常可以为您提供真正的原因.
When you get an exception you should also look at the message and the stack trace as this usually gives you the real cause.
这篇关于BigDecimal和BigInteger的限制是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!