skip to Main Content

I’m trying to code a program that approximates pi using a certain equation. However, when I go run the code it outputs 4.8731472566 instead of 3.1415924576 and I was wondering if there were any mistakes in my code. Here’s what I have so far

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

int main()
{
    int n, i;

    printf("n = ");
    scanf("%d", &n);

    double pi = 0.1; 
    double pi_1, pi_2, pi_3, pi_4, pi_5 = 0; //sets varibles that represent 4.0/((8*i)+1), etc. equal to zero 

    i = 0; // sets i equal to zero
    
    while(i <= n){ // starts a loop with i is less than non-negative interger n
        

        pi_1 = 4.0/((8*i)+1); //setting them equal to their respective parts of the equation
        pi_2 = 2.0/((8*i)+4);
        pi_3 = 1.0/((8*i)+5);
        pi_4 = 1.0/((8*i)+6);
        pi_5 = 1.0;

        int j = 0; //creating new interger j

        while(j <= i){ //as long as j isn't greater than i 
            pi_5 = pi_5*(1.0/16); //it will run the last part of the formula
            j++;
        }

        pi += pi * (pi_1 - pi_2 - pi_3 - pi_4 - pi_5); //sets pi = to the entire formula 
        i++;
    }
    
    

    printf("PI = %.10fn", pi);
    return 0;
}

And here is the equation…

enter image description here

I’ve tried changing pi to different integers like 0.001 and 1 and that didn’t work

3

Answers


  1. There are a few things wrong in the way you calculate pi. pi should start from 0.0 and pi += pi * (pi_1 - pi_2 - pi_3 - pi_4 - pi_5); should not contain the pi * part and you should multiply by pi_5 instead and the while loops condition should be j < i.


    I came up with this function (which makes use of <math.h>, instead of calculating the power with a loop). I am using k here since it is the same as used at the link I provided in the function to the formula.

    double approximatePI(int steps) {
        // https://en.wikipedia.org/wiki/Bailey–Borwein–Plouffe_formula
    
        double pi = 0.0;
    
        for (int k = 0; k < steps; ++k) {
            const double multiplicand = 1.0 / pow(16.0, k);
            const double minuend = 4.0 / (8.0 * k + 1.0);
    
            // if you add all of them and subtract then together its
            // the same result as if you subtract all of them individually
            const double subtrahend = 2.0 / (8.0 * k + 4.0) +
                                1.0 / (8.0 * k + 5.0) +
                                1.0 / (8.0 * k + 6.0);
    
            pi += multiplicand * (minuend - subtrahend);        
        }
    
        return pi;
    }
    

    Making this its own function makes it easier to use it in multiple spots. And btw if you post code on stackoverflow (or you just want to debug it yourself) it is easier and faster to test it if you provide some constant value for e.g. n (or what I called steps in my function) instead of using scanf() to get it from the user each time you run it.

    Login or Signup to reply.
  2. Reviewing the code, there seems to be a lot of unneeded variables and looping involved. I do see that there has been an answer added, but I thought I would go ahead and toss in my simplified version which also utilizes the math.h file and math library.

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main()
    {
        int i, n;
    
        printf("n = ");
        i = scanf("%d", &n);
    
        double pi = 0.0;
    
        for (i = 0; i <= n; i++)  // Use a simple "for loop" for the summation process
        {
            pi += (4.0/((8*i)+1) - 2.0/((8*i)+4) - 1.0/((8*i)+5) - 1.0/((8*i)+6)) / pow(16,i); // Sets pi = to the entire formula
        }
    
        printf("PI = %.10fn", pi);
        return 0;
    }
    

    A simple "for loop" is substituted in the refactored code, and using the summation formula, only the "pi" variable is necessary. Following was some sample output.

    craig@Vera:~/C_Programs/Console/PI/bin/Release$ ./PI
    n = 6
    PI = 3.1415926536
    

    The only additional thing to point out is that when using the "math.h" include file, a reference in the linking portion of the build needs to be done, usually with an "-lm" as "m" is the math function library.

    Login or Signup to reply.
  3. In addition to what others suggest:

    Tip: rather compute pi_5 with a somewhat expensive loop or with a slow 1/pow(16,i) call, use the prior value to quickly form the new one.

      double pi_5 = 1.0;
      for (int i = 0; i <= n; i++) {
        double pi_1 = 4.0 / ((8 * i) + 1);
        double pi_2 = 2.0 / ((8 * i) + 4);
        double pi_3 = 1.0 / ((8 * i) + 5);
        double pi_4 = 1.0 / ((8 * i) + 6);
        sum += (pi_1 - pi_2 - pi_3 - pi_4)*pi_5;
        pi_5 /= 16;
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search