我正在开发一个程序,以在 C++ 中分解非常大的数字(20 位或更多),并使用 GMP 来处理溢出问题。我的程序对于大约 10 位或更少的数字运行良好,但是当我向它扔一个 15 位数字时,它会爆炸。我将把我的程序简化为这样的一行:
#include <iostream>
#include <stdio.h>
#include <gmp.h>
#include <gmpxx.h>
using namespace std;
int main()
{
mpz_class n = 48112959837082048697; //this blows up
return 0;
}
如果我将该行替换为
mpz_class n = 12623773;
然后一切正常。
这是错误:
$ g++ -o main main.cpp -lgmpxx -lgmp
main.cpp: In function ‘int main()’:
main.cpp:21:19: error: conversion from ‘long long int’ to ‘mpz_class’ is ambiguous
/usr/include/gmpxx.h:1563:3: note: candidates are: __gmp_expr<__mpz_struct [1], __mpz_struct [1]>::__gmp_expr(double)
/usr/include/gmpxx.h:1562:3: note: __gmp_expr<__mpz_struct [1], __mpz_struct [1]>::__gmp_expr(float)
/usr/include/gmpxx.h:1560:3: note: __gmp_expr<__mpz_struct [1], __mpz_struct [1]>::__gmp_expr(long unsigned int)
/usr/include/gmpxx.h:1559:3: note: __gmp_expr<__mpz_struct [1], __mpz_struct [1]>::__gmp_expr(long int)
/usr/include/gmpxx.h:1557:3: note: __gmp_expr<__mpz_struct [1], __mpz_struct [1]>::__gmp_expr(short unsigned int)
/usr/include/gmpxx.h:1556:3: note: __gmp_expr<__mpz_struct [1], __mpz_struct [1]>::__gmp_expr(short int)
/usr/include/gmpxx.h:1554:3: note: __gmp_expr<__mpz_struct [1], __mpz_struct [1]>::__gmp_expr(unsigned int)
/usr/include/gmpxx.h:1553:3: note: __gmp_expr<__mpz_struct [1], __mpz_struct [1]>::__gmp_expr(int)
/usr/include/gmpxx.h:1551:3: note: __gmp_expr<__mpz_struct [1], __mpz_struct [1]>::__gmp_expr(unsigned char)
/usr/include/gmpxx.h:1550:3: note: __gmp_expr<__mpz_struct [1], __mpz_struct [1]>::__gmp_expr(signed char)
有人知道如何解决这个问题,以便我可以使用大数字吗?我认为 GMP 应该允许 500 位数字,正数或负数。
谢谢!