What are the limits of BigDecimal and BigInteger? [duplicate]

南笙酒味 提交于 2019-12-02 01:53:46

问题


I was multiplying very two huge BigIntegervalues in a program. It failed. What are the limits of BigInteger and BigDecimal ?


回答1:


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.

The limit is 32 * 2^32-1 bits for BigInteger or about 2^(4 billion).

You can get a NumberFormatException if you

  • create a BigInteger from an empty byte[]
  • use a signum < -1 or > +1
  • try to parse a number in base >36 or < 2
  • have a string with illegal digits.

When you get an exception you should also look at the message and the stack trace as this usually gives you the real cause.




回答2:


there shouldn't be a limit, except for memory, but maybe there is, according to the implementation of the class (for example, some fields there might be int or long).



来源:https://stackoverflow.com/questions/17945985/what-are-the-limits-of-bigdecimal-and-biginteger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!