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:
- define all variables
- set estimate of pi to be 0
- calculate a term from the taylor series and calculate the error in this term
- it then adds the latest term to the estimate of pi
- 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