The GNU Multiple Precision Arithmetic Library


I built and installed the GNU Multiple Precision Arithmetic Library in my directory on the Cluster. The plan is to install it in a more general setting but in the meantime you should be able to access it as follows:
  1. Add the lines:
        export PATH=.:$PATH;
        C_INCLUDE_PATH=/home/siegelj/bigInt/gmp-6.1.1;
        export C_INCLUDE_PATH;
    to your .bashrc
     
  2. Build with
        gcc <your_code>.c -o <your_code> /home/siegelj/bigInt/gmp-6.1.1/.libs/libgmp.a
There is also a very useful on-line manual.

A Run


The Code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "gmp.h"
int main (int argc, char* argv[]){
    mpz_t int1,int2,int3,int_result;
    char string_buf[50];
    /*Initialize the big integers*/
    mpz_init (int1);
    mpz_init (int2);
    mpz_init (int3);
    mpz_init(int_result);
    /*The inputs are strings that have to be converted*/
    printf("input first number(x)\n");
    scanf("%s",string_buf);
    mpz_set_str (int1,string_buf, 10);
    printf("input second number(y)\n");
    scanf("%s",string_buf);
    mpz_set_str (int2,string_buf, 10);
    printf("input third number(y)\n");
    scanf("%s",string_buf);
    mpz_set_str (int3,string_buf, 10);
    /*Calculate the sum, difference, product, quotient and remainder*/
    mpz_add(int_result,int1,int2);
    printf("x+y=");
    mpz_out_str (stdout, 10, int_result);
    printf("\n");
    mpz_sub(int_result,int1,int2);
    printf("x-y=");
    mpz_out_str (stdout, 10, int_result);
    printf("\n");
    printf("x*y=");
    mpz_mul(int_result,int1,int2);
    mpz_out_str (stdout, 10, int_result);
    printf("\n");
    printf("x/y=");
    mpz_tdiv_q(int_result,int1,int2);
    mpz_out_str (stdout, 10, int_result);
    printf("\n");
    printf("mod(x^y,z)=");
    mpz_powm (int_result,int1,int2,int3);
    mpz_out_str (stdout, 10, int_result);
    printf("\n");
    /* mpz_powm (MP_INT *res, MP_INT *base, MP_INT *exp, MP_INT *mod);*/
    printf("rem(x,y)=");
    mpz_tdiv_r(int_result,int1,int2);
    mpz_out_str (stdout, 10, int_result);
    printf("\n");
    return 0;
}