BigInteger没有限制意味着什么?

时间:2021-10-03 16:48:59

I looked into this * question relating to Big Integer and specifically I do not understand this line (the words in italics):

我查看了这个与Big Integer有关的*问题,特别是我不理解这一行(斜体字):

In the BigInteger class, I have no limits and there are some helpful functions there but it is pretty depressing to convert your beautiful code to work with the BigInteger class, specially when primitive operators don't work there and you must use functions from this class.

在BigInteger类中,我没有限制,并且有一些有用的函数,但将你漂亮的代码转换为使用BigInteger类是非常令人沮丧的,特别是当原始操作符不能在那里工作时你必须使用这个类中的函数。

I don't know what I am missing but to represent something that has no limit you would require infinite memory ? Whats is the trick here ?

我不知道我错过了什么但是代表一些没有限制的东西你需要无限的记忆?这是什么诀窍?

4 个解决方案

#1


70  

There is no theoretical limit. The BigInteger class allocates as much memory as it needs for all the bits of data it is asked to hold.

没有理论上的限制。 BigInteger类为它要求保存的所有数据位分配所需的内存。

There are, however, some practical limits, dictated by the memory available. And there are further technical limits, although you're very unlikely to be affected: some methods assume that the bits are addressable by int indexes, so things will start to break when you go above Integer.MAX_VALUE bits.

但是,有一些实际的限制,由可用的内存决定。还有一些技术限制,尽管你不太可能受到影响:有些方法假设这些位可以被int索引寻址,所以当你超过Integer.MAX_VALUE位时,事情就会开始破坏。

#2


13  

Graham gave great answer to this question. I would like only to add that you have to be carefull with valueOf method because it is created using long parameter so the maximum value is Long.MAX_VALUE.

格雷厄姆对这个问题给出了很好的答案。我只想补充一点,你必须小心使用valueOf方法,因为它是使用long参数创建的,因此最大值为Long.MAX_VALUE。

#3


3  

Yes its used when we need very big numbers with arbitrary precision. It's important to note that "arbitrary" precision or number of digits does not mean "unlimited": it means that the number of digits in a number or number of digits of precision in a calculation is limited by memory and/or defined limits to precision that we specify.

是的,当我们需要具有任意精度的非常大的数字时使用它。重要的是要注意“任意”精度或数字位数并不意味着“无限制”:它意味着计算中精度数字或位数的位数受内存和/或定义的精度限制的限制我们指定的。

#4


2  

Look at the BigInteger class source code, you will see (it can be done with NetBean). A number will be represented as an int arrays. Example, 10113 will be [1, 0, 1, 1, 3] (this is not exactly what the BigInteger class does, just an example how big number module work). So, technically, its only limit will be your memory.

查看BigInteger类源代码,您将看到(可以使用NetBean完成)。数字将表示为int数组。例如,10113将是[1,0,1,1,3](这不是BigInteger类所做的,只是大数模块工作的一个例子)。所以,从技术上讲,它唯一的限制就是你的记忆。

#1


70  

There is no theoretical limit. The BigInteger class allocates as much memory as it needs for all the bits of data it is asked to hold.

没有理论上的限制。 BigInteger类为它要求保存的所有数据位分配所需的内存。

There are, however, some practical limits, dictated by the memory available. And there are further technical limits, although you're very unlikely to be affected: some methods assume that the bits are addressable by int indexes, so things will start to break when you go above Integer.MAX_VALUE bits.

但是,有一些实际的限制,由可用的内存决定。还有一些技术限制,尽管你不太可能受到影响:有些方法假设这些位可以被int索引寻址,所以当你超过Integer.MAX_VALUE位时,事情就会开始破坏。

#2


13  

Graham gave great answer to this question. I would like only to add that you have to be carefull with valueOf method because it is created using long parameter so the maximum value is Long.MAX_VALUE.

格雷厄姆对这个问题给出了很好的答案。我只想补充一点,你必须小心使用valueOf方法,因为它是使用long参数创建的,因此最大值为Long.MAX_VALUE。

#3


3  

Yes its used when we need very big numbers with arbitrary precision. It's important to note that "arbitrary" precision or number of digits does not mean "unlimited": it means that the number of digits in a number or number of digits of precision in a calculation is limited by memory and/or defined limits to precision that we specify.

是的,当我们需要具有任意精度的非常大的数字时使用它。重要的是要注意“任意”精度或数字位数并不意味着“无限制”:它意味着计算中精度数字或位数的位数受内存和/或定义的精度限制的限制我们指定的。

#4


2  

Look at the BigInteger class source code, you will see (it can be done with NetBean). A number will be represented as an int arrays. Example, 10113 will be [1, 0, 1, 1, 3] (this is not exactly what the BigInteger class does, just an example how big number module work). So, technically, its only limit will be your memory.

查看BigInteger类源代码,您将看到(可以使用NetBean完成)。数字将表示为int数组。例如,10113将是[1,0,1,1,3](这不是BigInteger类所做的,只是大数模块工作的一个例子)。所以,从技术上讲,它唯一的限制就是你的记忆。