Thread: pi program using arbitrary precision (GMP)

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    19

    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_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.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    19
    Ah, sorry about that. Still doesn't work with mpf_mul_ui (x,x,2);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 07-12-2011, 08:00 PM
  2. Arbitrary Precision C++
    By bsmath in forum C++ Programming
    Replies: 1
    Last Post: 02-07-2011, 04:35 PM
  3. Functions of arbitrary parameters
    By Nextstopearth in forum C Programming
    Replies: 2
    Last Post: 06-16-2009, 03:15 AM
  4. arbitrary precision and operator overloading
    By scwizzo in forum C++ Programming
    Replies: 8
    Last Post: 04-22-2009, 09:48 AM
  5. Pointing to arbitrary location
    By elmutt in forum C Programming
    Replies: 2
    Last Post: 02-28-2008, 06:30 AM