開源演算法庫GMP的安裝與調試

來源:互聯網
上載者:User

gmp簡介:
GMP是一個任意精度的開源算術庫,可用於符號整數,有理數,浮點數計算。算數庫對於有沒有實際的限制,唯一的限制是電腦的記憶體。 GMP具有豐富的函數集並且函數都有通用的介面。

gmp的安裝:

環境:ubuntu11.10

Terminal中運行:
sudo apt-get install libgmp3-dev
gmp的調試:
建立.c檔案,輸入一下代碼。
#include<gmp.h>
#include<stdio.h>
void main()
{
        mpz_t s,n;
        int i;

        mpz_init(s);//init s,the value is 0
        mpz_init(n);

        for(i=1;i<1111111111;i++)
        {
                mpz_add_ui(n,n,1);//set n to n+1
                mpz_addmul(s,n,n);//add n*n to s
        }

        gmp_printf("the sum is %Zd\n",s);

        mpz_clear(s);
}
Termial 中編譯:
gcc test.c -o test -lgmp
Termial中運行:
./test
算了大概3分鐘,得到結果:
the sum is 457247370073159579654778235

函數封裝:
封裝的目的是給出通用的介面,下面實現的就是大數的乘法。
#include<gmp.h>
#include<stdio.h>
char* BigMul(char* m,char* n);
void main()
{
char* p=NULL;
char *a="12345678";
char *b="23456789";
p=BigMul(a,b);
printf("the result is %s./n",p);
}
char* BigMul(char* m,char* n)
{
int i,j;
char* pt=NULL;
mpz_t s,p,q;
        mpz_init(s);
i=mpz_init_set_str(p,m,10);//get number from m
j=mpz_init_set_str(q,n,10);
//printf("i,j:%d,%d\n",i,j);
gmp_printf("%Zd\n%Zd\n",p,q);
mpz_addmul(s,p,q);//calculate result
//gmp_printf("the result is %Zd\n",s);
pt=mpz_get_str(pt,10,s);//get string from s
//printf("%s\n",pt);
mpz_clear(s);
return pt;
}
通過兩個字串變數將乘數傳進去,再傳回結果指標。
算得結果:
the result is 289589963907942.
下一步是在sipesc力學平台上做成外掛程式。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.