GMP introduction:
GMP is an open-source arithmetic library with any precision. It can be used for symbol integers, rational numbers, and floating point numbers. Does the arithmetic library have actual restrictions? The only limitation is the computer memory. GMP has rich function sets and functions all have common interfaces.
Installation of GMP:
Environment: ubuntu11.10
Run in terminal:
Sudo apt-Get install libgmp3-dev
GMP debugging:
Create a. c file and enterCode.
# 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 );
}
Compile in termial:
GCC test. C-o Test-lgmp
Run in termial:
./Test
It takes about 3 minutes. The result is as follows:
The sum is 457247370073159579654778235
Function encapsulation:
The purpose of encapsulation is to provide a common interface. The following is the multiplication of large numbers.
# 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", 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;
}
Two string variables are used to transmit the data and then return the result pointer.
Calculation Result:
The result is 289589963907942.
The next step is to build a plug-in on the sipesc mechanics platform.