C++ GMP常用函数(mpz_class有符号整数)
1.构造函数 (mpz_class类)
由于构造函数都是explict因此不会隐式类型转化
- explicit mpz_class::mpz_class (const char *s, int base = 0)
- explicit mpz_class::mpz_class (const string& s, int base = 0)
# If the string is not a valid integer, an std::invalid_argument exception is thrown. 默认为10进制
- mpz_class x(z); #z为mpz_t类型
Construct an mpz_class converted from a string using mpz_set_str
将字符串转化为有符号整数,base为进制数
Exa:
mpz_class z = mpz_class("-20",16);
cout<<z<<endl; //输出-32
2.类型转换
- mpz_t mpz_class::get_mpz_t ()
- mpq_t mpq_class::get_mpq_t ()
- mpf_t mpf_class::get_mpf_t ()
- mpz_gcd (a.get_mpz_t(), b.get_mpz_t(), c.get_mpz_t()); # mpz_class必须化为mpz_t
These can be used to call a C function which doesn’t have a C++ class interface.
For example to set a to the GCD of b and c,
3.整数类型的简单的加减乘除
//一般的操作都被封装好了 和 int等基本数据类型
#include <iostream>
#include <gmp.h>
#include <gmpxx.h>
using namespace std;
int main(int argc, char **argv) {
{
mpz_class a = 1231232131;
mpz_class b = 21321321;
mpz_class c = 2312321;
mpz_class d;
d = a+b;
cout<<d<<endl;
d = a-b;
cout<<d<<endl;
d = a*b*c;
cout<<d<<endl;
d = a/b;
cout<<d<<endl;
return 0;
}
}
输出:
/Users/zhao/CLionProjects/MyGMP/cmake-build-debug/MyGMP
1252553452
1209910810
60701884304238869293371
57
Process finished with exit code 0