c++ - boost-multi precision cpp_int 的最高限制是多少?

标签 c++ biginteger boost-multiprecision

我正在研究大型半素数的因式分解。我正在研究 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++可以直接使用指针,所以最大尺寸为cpp_int在 C++ 中,与指针可以寻址的最大内存范围成正比,在 64 位体系结构中通常但不总是™ 264-1;或者换句话说,cpp_int 的最大值是 2264-1-1,根据他们如何实现该符号来调整或调整一个数量级。

在支持更大指针(或可以寻址更大内存范围的指针)的环境中,最大值可能更大。

实际上,最大值为 cpp_int (就此而言,Java 的 BigInteger )是运行时环境允许分配多少内存的实际限制。

关于c++ - boost-multi precision cpp_int 的最高限制是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56582824/

相关文章:

c++ - g++ 与 GSL 的链接问题

java - Jace::虚拟机关机错误

C# 随机 BigInt 生成器

java - java中的任意数组长度

c++ - boost::多精度::数字

c++ - 模板 - 练习

android - cpp中的类变量初始化

kotlin - 未解析的引用 : [BigInteger]. longValue

c++ - 无法在 Windows 上使用英特尔编译器编译示例 boost::multiprecision

python - C++ Boost Multiprecision 与 Python 的 mpmath 之间的互操作性