c++ - boost::multiprecision::cpp_int 表现得像 unsigned long long

标签 c++ boost boost-multiprecision

<分区>

所以我有一个编程作业,其中我必须处理 64 位数字。我目前正在使用 boost::multiprecision::cpp_int 库,但我无法在大量情况下使用它。

例如-

#include <boost/multiprecision/cpp_int.hpp>

int main()
{
    boost::multiprecision::cpp_int n1 =123341257612045876129038576124390847381295732;   
}

这是代码太长的错误。 经过实验,似乎 cpp_int(应该包含任意大的数字)就像一个 unsigned long long(如上图所示),我该如何解决这个问题?

最佳答案

使用字符串构造函数,因为你不能在 C++ 中表达初始化器,否则。

Live On Coliru

#include <boost/multiprecision/cpp_int.hpp>
#include <iostream>

int main() {
    boost::multiprecision::cpp_int n1("123341257612045876129038576124390847381295732");
    n1 *= n1;
    n1 *= n1;

    std::cout << n1 << "\n";
}

打印

231437371927256216552064578371685752056581253909626001052591740261122589692668963795268703088073326299461305156397520102373182511147316463882992573577585984095769469664077598976

也就是 ~2.31 × 10^176

关于c++ - boost::multiprecision::cpp_int 表现得像 unsigned long long,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72120029/

相关文章:

c++ - 如何从c中的dll调用源代码中的函数

c++ - 通过转换 boost 状态图传递参数

c++ - boost::ptr_vector 和指针

c++ - 从构造函数调用 shared_from_this

c++ - 加载RFC5114中定义的1024bit P和G值到boost uint1024

c++ - 从 'int' 到 'const char*' [-fpermissive] 的无效转换

c++ - 在 Boost.Geometry rtree 中存储 OGRPoint

c++ - 如何以二进制形式显示C++ Boost Library的多精度大整数?

c++ - C++ 中的 ifstream 对象数组