# Thread: pi program using arbitrary precision (GMP)

1. ## pi program using arbitrary precision (GMP)

HI everyone,

I'm trying to make a program to compute pi to n places. I'm using the Gauss Legendre (Gauss-Legendre Algorithm -- from Harry J. Smith).
I'm using the GMP MP package for arbitrary precision. The variables correspond to those in that link

Code:
```
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <numeric>
#include <ctime>
#include <iomanip>
#include <cmath>

#include <gmp.h>
#include <gmpxx.h>

using namespace std;

int main()

{
//set a//

mpf_set_default_prec(100000);              //bit precision

mpf_t a;
mpf_init (a);
mpf_set_ui (a,1);

//set b//
mpf_t b;
mpf_init (b);

mpf_t b1;
mpf_init (b1);
mpf_sqrt_ui (b1,2);

mpf_ui_div(b,1,b1);

//set t//

mpf_t t;
mpf_init (t);
mpf_set_d (t,0.25);

//set x//

mpf_t x;
mpf_init (x);
mpf_set_ui(x,1);

//calc//
for (int f=0;f<1000;f++)                    //number of iterations
{

//set y//
mpf_t y;
mpf_init (y);
mpf_set (y,a);

//change a//

mpf_t a1;
mpf_init (a1);

mpf_div_ui (a,a1,2);

// change b//

mpf_t b1;
mpf_init (b1);
mpf_mul(b1,b,y);

mpf_sqrt (b,b1);

//set t//

mpf_t t1;
mpf_init(t1);
mpf_sub(t1,y,a);

mpf_t t2;
mpf_init (t2);
mpf_pow_ui (t2,t1,2);

mpf_t t3;
mpf_init(t3);
mpf_mul(t3,x,t2);

mpf_sub(t,t,t2);

//change x//

}

// calculate pi//

mpf_t piup1;
mpf_init(piup1);

mpf_t piup2;
mpf_init (piup2);
mpf_pow_ui(piup2,piup1,2);         //final up

mpf_t pidown;                   //final down
mpf_init (pidown);
mpf_mul_ui(pidown,t,4);

mpf_t pifinal;
mpf_init (pifinal);
mpf_div(pifinal,piup2,pidown);

cout << setprecision(100)<<pifinal;  //output pi//

}```
The problem I'm having is that despite many iterations the number is incorrect at 3.1410420453.

Any help is most appreciated.

2. Aside from the ridiculous amount of blank lines (you only need 1 for blocks of code, and perhaps 2 between functions), your "//change x//" has NO code.

3. Ah, sorry about that. Still doesn't work with mpf_mul_ui (x,x,2);