Search code examples
c++loopsdo-whilepitaylor-series

Function for calculating Pi using taylor series in c++


So I'm at a loss on why my code isn't working, essentially the function I am writing calculates an estimate for Pi using the taylor series, it just crashes whenever I try run the program.

here is my code

#include <iostream>
#include <math.h>
#include <stdlib.h>
using namespace std;

double get_pi(double accuracy)
{
double estimate_of_pi, latest_term, estimated_error;
int sign = -1;
int n;

estimate_of_pi = 0;
n = 0;

do
{
    sign = -sign;
    estimated_error = 4 * abs(1.0 / (2*n + 1.0));  //equation for error
    latest_term = 4 * (1.0 *(2.0 * n + 1.0));      //calculation for latest term in series
    estimate_of_pi = estimate_of_pi + latest_term; //adding latest term to estimate of pi
    n = n + 1;                                     //changing value of n for next run of the loop
}
while(abs(latest_term)< estimated_error);

return get_pi(accuracy);

}

int main()
 {
    cout << get_pi(100);
 }

the logic behind the code is the following:

  1. define all variables
  2. set estimate of pi to be 0
  3. calculate a term from the taylor series and calculate the error in this term
  4. it then adds the latest term to the estimate of pi
  5. the program should then work out the next term in the series and the error in it and add it to the estimate of pi, until the condition in the while statement is satisfied

Thanks for any help I might get


Solution

  • There are several errors in your function. See my comments with lines starting with "//NOTE:".

    double get_pi(double accuracy)
    {
       double estimate_of_pi, latest_term, estimated_error;
       int sign = -1;
       int n;
    
       estimate_of_pi = 0;
       n = 0;
    
       do
       {
          sign = -sign;
    
          //NOTE: This is an unnecessary line.
          estimated_error = 4 * abs(1.0 / (2*n + 1.0));  //equation for error
    
          //NOTE: You have encoded the formula incorrectly.
          // The RHS needs to be  "sign*4 * (1.0 /(2.0 * n + 1.0))"
          //                       ^^^^          ^
          latest_term = 4 * (1.0 *(2.0 * n + 1.0));      //calculation for latest term in series
          estimate_of_pi = estimate_of_pi + latest_term; //adding latest term to estimate of pi
          n = n + 1;                                     //changing value of n for next run of the loop
       }
    
       //NOTE: The comparison is wrong.
       // The conditional needs to be "fabs(latest_term) > estimated_error"
       //                              ^^^^             ^^^
       while(abs(latest_term)< estimated_error);
    
       //NOTE: You are calling the function again.
       // This leads to infinite recursion.
       // It needs to be  "return estimate_of_pi;"
       return get_pi(accuracy);    
    }
    

    Also, the function call in main is wrong. It needs to be:

    get_pi(0.001) 
    

    to indicate that if the absolute value of the term is less then 0.001, the function can return.

    Here's an updated version of the function that works for me.

    double get_pi(double accuracy)
    {
       double estimate_of_pi, latest_term;
       int sign = -1;
       int n;
    
       estimate_of_pi = 0;
       n = 0;
    
       do
       {
          sign = -sign;
          latest_term = sign * 4 * (1.0 /(2.0 * n + 1.0));  //calculation for latest term in series
          estimate_of_pi += latest_term;                    //adding latest term to estimate of pi
          ++n;                                              //changing value of n for next run of the loop
       }
       while(fabs(latest_term) > accuracy);
    
       return estimate_of_pi;
    }