1

Okay, I want to make a C program that calculates pi accurately to 4th decimal place (3.1415...). I thought that double is more accurate than float type... Even with a trillion terms (n=trillion), the program cannot go past 3.1414... Can someone help? Am I using an incorrect data type to store my Pi value or is my loops incorrect?

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char** argv) {
    int n;
    while(1){
        printf("Please enter how many terms (n) you wish to add to approximate Pi: ");
        scanf("%d", &n);
        if(n>=1)
            break;
    }

    int x;
    int count =2;
    double negSum=0;
    double posSum=0;
    double pi = 0;
    for(x=1;x<=n;x++){
        do{
            if(x%2==1){
                posSum += (4.0)/(2.0*x-1.0);
                count++;
            }
            else{
                negSum += (-4.0)/(2.0*x-1.0);
                count++;
            }
            pi = negSum + posSum;
        }
        while(pi>3.1414999 && pi<3.14160000);
    }
    //pi = negSum + posSum;
    printf("The value of Pi using your approximation is %f, and the iteration was %d", pi, count);
    return (EXIT_SUCCESS);
}

Here is some of my sample input/output:

Please enter how many terms (n) you wish to add to approximate Pi: 98713485
The value of Pi using your approximation is 3.141407, and the iteration was 98713488
Community
  • 1
  • 1
user3326078
  • 617
  • 1
  • 9
  • 19
  • 5
    Why are you continuing the approximation `while(pi>3.1414999 && pi<3.14160000);`? Shouldn’t it be the other way around? Also, you don’t need two different running totals. Just add to `pi`. – Ry- Sep 13 '14 at 20:26
  • Why do you have two loops? – Barmar Sep 13 '14 at 20:33
  • 1
    [Using basic arithmetics for calculating PI](http://stackoverflow.com/questions/4484489/using-basic-arithmetics-for-calculating-pi-with-arbitary-precision) has links to various options for calculation. – Richard Chambers Sep 13 '14 at 20:40
  • Please do not vandalize your own question. Rolled back. – Jongware Sep 14 '14 at 00:26

2 Answers2

4

The series you are using:

pi = 4(1 - 1/3 + 1/5 - 1/7 + 1/9 ...)

converges REALLY slowly to pi. It is the evaluation of a Taylor series for 4arctan(x) at x=1 and converges conditionally (it is right on edge of the interval of convergence). That's not going to be a very numerically efficient way to compute pi.

Beyond that, I haven't carefully checked your implementation, but some others have pointed out problems in the comments.

Jeremy West
  • 11,495
  • 1
  • 18
  • 25
3

To compute Pi to 4th decimal place, you could use Gauss-Legendre algorithm:

#include <math.h>
#include <stdio.h>

int main(void) {
  const double PI = acos(-1), SQRT2 = sqrt(2.0);
  double a = 1, b = 1/SQRT2, t = .25, p = 1;
  double an, piold, pi = 1, eps = 1e-6; /* use +2 decimal places */
  int iteration_count = 0;
  do {
    ++iteration_count;
    an = .5 * (a + b);
    b = sqrt(a * b);
    t -= p * (a - an) * (a - an);
    a = an;
    p *= 2;
    piold = pi;
    pi = (a + b) * (a + b) / (4 * t);
  } while (fabs(pi - piold) > eps);

  printf("got pi=%f with rel. err=%.2e in %d iterations\n",
         pi, (pi - PI) / PI, iteration_count);
  return 0;
}

To run it:

$ gcc  *.c -lm && ./a.out

Output

got pi=3.141593 with rel. err=2.83e-16 in 3 iterations
Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670