boost::multiprecision::cpp_int的行为类似于无符号长整型



所以我有一个编程任务,其中我必须使用64位数字。我目前正在使用boost::multiprecision::cpp_int库,但无法在大数字上使用它。

例如

#include <boost/multiprecision/cpp_int.hpp>
int main()
{
boost::multiprecision::cpp_int n1 =123341257612045876129038576124390847381295732;   
}

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

使用字符串构造函数,因为不能用C++表达初始值设定项,否则。

在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

最新更新