C++ GMP常用函数

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

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值