我正在研究分解大的半素数。我正在研究Java,但我也很好奇也探索其他选择。我知道C++ Boost多精度支持大量数字。
我从Boost页面找到了以下信息:
typedef number<cpp_int_backend<128, 128, unsigned_magnitude, unchecked, void> > uint128_t;
typedef number<cpp_int_backend<256, 256, unsigned_magnitude, unchecked, void> > uint256_t;
typedef number<cpp_int_backend<512, 512, unsigned_magnitude, unchecked, void> > uint512_t;
typedef number<cpp_int_backend<1024, 1024, unsigned_magnitude, unchecked, void> > uint1024_t;
我的问题是,cpp_int类型的最大数量限制是多少?在Java中,BigInteger最多支持2 ^ Integer.MAX_VALUE。
谢谢。
最佳答案
cpp_int
没有[理论]最大值
重要的是要记住,在Java中,值的最大大小受Java虚拟机表示值的物理限制所限制。在基础实现中,Java(可能)通过以下方式实现BigInteger
:
public class BigInteger {
private long[] bits;//limited by the maximum size of an array in Java
/*...*/
}
免责声明:我不知道他们使用long[]
还是int[]
来存储位。类似地,在C++中,一旦您删除了抽象,
cpp_int
就会以非常相似的结构实现:class boost::multiprecision::cpp_int {
uint64_t * bits;
uint64_t * end; //Points to the element-after-the-last in bits
/*...*/
};
再次免责声明:他们可能会做些比这更聪明的事情。Java受到理论上的限制的事实是,JVM在
Integer.MAX_VALUE
上对数组大小设置了硬上限,因为数组是使用int
而不是long
进行索引的。 C++可以直接使用指针,因此C++中cpp_int
的最大大小与指针可以寻址的最大内存范围成比例-在64位体系结构中,通常为但并非总是™264-1。换句话说,cpp_int
的最大值为2264-1-1,根据其实现符号的方式给定或接受一个数量级。在支持更大的指针(或可以寻址更大内存范围的指针)的环境中,最大值可能更大。
实际上,
cpp_int
(和Java的BigInteger
)的最大值是对运行时环境允许分配多少内存的实际限制。关于c++ - boost-multiprecision cpp_int的最高限制是多少?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56582824/