c++ - 具有模数的boost多精度库不一致

标签 c++ boost biginteger

我发现使用 cpp_int 的 boost 多精度库有些不一致,想知道问题是否出在我这边?我做错了什么吗?

boost::multiprecision::cpp_int value("845812507058753702096720396260955981034309941487979439207575316627396775257009179367680598562088782400182102510047921049667535737841056751035898984440045398065941794853342721440022891483618946596390530332584847468817849746783423105644934675762519035784877729169739110084935079201004991911753548016158266946593610497793934212345180527788034865286995713462176706647193473406223095268503330593499438446017000593156395272905592017851490768402042283892535127698736772114426168690580061412400354553387531076676433901465842118416610671452446364936252601684680593015917270112975907856081311621268680168563153055479531193987696015767888543608430149655940111761214342848772129089336344636193634262254610730");
boost::multiprecision::cpp_int residueResult = value % 733;
std::cout << residueResult;                      // this prints out 4
int residue1 = residueResult.convert_to<int>();  // this is 4
int residue2 = int(value % 733);                 // this is 1

为什么在执行 int(value %733) 时它给出的值为 1?

这是 boost 1.59.0 和 visual studio 2013 社区。

最佳答案

手头的问题是 value % 733 是一些实现细节代理类型,您不能只将其转换为 int。事实上,它甚至不能用 gcc 和 clang 编译,正如你所看到的,它在 MSVC 中产生了无意义的结果。要解决这个问题,请在转换之前转换回 cpp_int:

int main () {
    boost::multiprecision::cpp_int value("845812507058753702096720396260955981034309941487979439207575316627396775257009179367680598562088782400182102510047921049667535737841056751035898984440045398065941794853342721440022891483618946596390530332584847468817849746783423105644934675762519035784877729169739110084935079201004991911753548016158266946593610497793934212345180527788034865286995713462176706647193473406223095268503330593499438446017000593156395272905592017851490768402042283892535127698736772114426168690580061412400354553387531076676433901465842118416610671452446364936252601684680593015917270112975907856081311621268680168563153055479531193987696015767888543608430149655940111761214342848772129089336344636193634262254610730");
    boost::multiprecision::cpp_int residueResult = value % 733;
    std::cout << residueResult << "\n";                      // this prints out 4
    int residue1 = residueResult.convert_to<int>();  // this is 4
    int residue2 = static_cast<int>(boost::multiprecision::cpp_int(value % 733));                

    std::cout << residue1 << "\n" << residue2;
}

这将打印

4
4
4

根据需要。

关于c++ - 具有模数的boost多精度库不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32937797/

相关文章:

c++ - 使用 boost::successive_shortest_path_nonnegative_weights 的最小成本最大流

c++ - 为什么 boost 如此大量地模板化?

c++ - 在映射中搜索元组的一部分作为键

C++:快速确定适当的 header 包含列表?

c++ - boost.python 公开返回 vector<MyClass> 的函数

javascript - 在 Javascript 中将字符串转换为大整数?

java - Java 中带有 BigInteger 的 StackOverFlowError

c++ - 基于客户端服务器的多人游戏的最佳更新频率

C++ "Variable not declared in this scope"- 再次

c# - Mono.Math.BigInteger 由于其保护级别而无法访问