PDA

View Full Version : How do I use GMP?



bns
May 30th, 2007, 06:54 PM
I have written a program in C++. It works fine for small numbers, but the numbers become large quite quickly. It seems like the GMP library will do what I need, but I don't really understand how to implement it (I'm really inexperienced). The manual is difficult to understand, and I was wondering if anyone knows of an understandable howto, or even just a couple of examples of simple code that utilize GMP.

tkjacobsen
May 31st, 2007, 09:00 PM
Note that this was the first time I used gmp, so it might not be perfect or even correct. But it calculates pi correctly to 80 digits (I dont want to check more)

This is a program calculating pi using the gauss-legendre algorithm. It works with arbitrary precision.


#include<gmpxx.h>
#include<iostream>
#include<iomanip>


int main()
{
long prec = 1000;
mpf_set_default_prec(prec);

mpf_class a(1);
mpf_class b(mpf_class(1)/sqrt(mpf_class(2)));
mpf_class t(mpf_class(1)/mpf_class(4));
mpf_class p(1);
mpf_class x,y,pi;

while ( a - b > mpf_class(1e-1000))
{
x = (a + b)/2;
y = sqrt(a*b);
t = t - p*(a-x)*(a-x);
a = x;
b = y;
p *=2;
}

pi = (a+b)*(a+b)/(mpf_class(4)*t);

std::cout << std::setprecision(80) << pi << std::endl;

return 0;
}


To compile:
g++ -lgmp -lgmpxx pi.cpp -o pi


EDIT:
you can find a manual here:
http://www.gnu.org/software/gmp/manual/

bns
June 1st, 2007, 01:27 AM
Thanks! I haven't had a chance to look over it yet, but I appreciate it.

WW
June 1st, 2007, 03:25 AM
If you are considering C++, take a look at CLN (http://www.ginac.de/CLN/).

bns
June 1st, 2007, 02:50 PM
Thanks, I'll look into it.

bns
June 1st, 2007, 04:35 PM
I got it to work using GMP, thanks for the help.