zoukankan      html  css  js  c++  java
  • 高精度运算库gmp

    网址:www.gmplib.org

    我下载的是 6.1.2版本:https://gmplib.org/download/gmp/gmp-6.1.2.tar.bz2

    执行操作如下:

    1.  tar -jvxf gmp-6.1.2.tar.bz2
    2.  ./configure --enable-cxx
    注意:在configure的时候一定要加上--enable-cxx,否则不能使用C++库gmpxx.h
    3.  make
    4.  make check
    5.  sudo make intall


    用c语言编一个例子:

    #include<gmpxx.h>
    using namespace std;
    int main()
    {
        mpz_t a, b, c, d;
        mpz_init(a);
        mpz_init(b);
        mpz_init(c);
        mpz_init(d);
        //计算2的1000次方
        mpz_init_set_ui(a, 2);
        mpz_pow_ui(c, a, 1000);
        gmp_printf("c = %Zd
    ", c);
        
        //计算12345678900987654321*98765432100123456789
        mpz_init_set_str(b, "12345678900987654321", 10);//10进制 
        mpz_init_set_str(c, "98765432100123456789", 10);
        mpz_mul(d, b, c);
        gmp_printf("d = %Zd
    ", d);
        mpz_clear(a);
        mpz_clear(b);
        mpz_clear(c);
        mpz_clear(d);
        return 0;
    }

    用c++要简单很多:

    #include<gmpxx.h>
    using namespace std;
    int main()
    {
        mpz_t a, b, c, d;
        mpz_init(a);
        mpz_init(b);
        mpz_init(c);
        mpz_init(d);
        //计算2的1000次方
        mpz_init_set_ui(a, 2);
        mpz_pow_ui(c, a, 1000);
        gmp_printf("c = %Zd
    ", c);
        
        //计算12345678900987654321*98765432100123456789
        mpz_init_set_str(b, "12345678900987654321", 10);//10进制 
        mpz_init_set_str(c, "98765432100123456789", 10);
        mpz_mul(d, b, c);
        gmp_printf("d = %Zd
    ", d);
        mpz_clear(a);
        mpz_clear(b);
        mpz_clear(c);
        mpz_clear(d);
        return 0;
    }

    注意,编译时要链接gmp库:

    g++ name.cpp -o name.o -lgmpxx -lgmp
  • 相关阅读:
    html之marquee详解
    CSS盒模型
    基于windows API的手柄/键盘映射编程(一)
    阿超的烦恼来临的始端
    阿超的小目标
    程序员的800字作文
    Link to Coding
    项目经理都干些什么啊
    停不下来的英语课联想
    Markdown
  • 原文地址:https://www.cnblogs.com/litifeng/p/10486085.html
Copyright © 2011-2022 走看看