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_add(a1,a,b);
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_add(piup1,a,b);
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.