#include<cmath>
#include<fstream>
#include<iostream>
#include<cstdlib>
usingnamespace std;
int main ()
{
double x = 1;
double PI = 4*atan(x);
double aPI = 0;
double error;
double neg1 = -1;
for(int N = 0; N <= 15; N++)
{
aPI=0;
for(int k = 0; k <= N; k++)
{
aPI = aPI + pow(neg1,k) * ((4) / (2*k+1));
error = (aPI - PI) * 100/PI;
//cout << "The value of pi through the leibniz formula is " << aPI << endl;
cout << "The percent error from the calculated and actual value is " << error << endl;
cout << N << endl;
}
}
return 0;
}
#include<cmath>
#include<fstream>
#include<iostream>
#include<cstdlib>
using namespace std;
int main ()
{
double x = 1;
double PI = 4*atan(x);
double aPI = 0;
double error;
double neg1 = -1;
for(int N = 0; N <= 15; N++)
{
for(int k = 0; k <= N; k++)
aPI = aPI + pow(neg1,k) * ((4) / (2*k+1));
error = (aPI - PI) * 100/PI;
//cout << "The value of pi through the leibniz formula is " << aPI << endl;
cout << "The percent error from the calculated and actual value is " << error << endl;
cout << N << endl;
}
return 0;
}
here is a more refined code. But i don't know how to get a more accurate approximation of pi. I'm suppose to be using the leibniz formula to calculate it but instead of getting closer to 3.14 it is increasing by 3 each time. how would i go about fixing this?
#include<cmath>
#include<fstream>
#include<iostream>
#include<cstdlib>
usingnamespace std;
int main ()
{
double x = 1;
double PI = 4*atan(x);
double aPI = 0;
double error;
double neg1 = -1;
for(int N = 0; N <= 15; N++)
{
for(int k = 0; k <= N; k++)
{
aPI = aPI + (pow(neg1,k) * (4/(2*k+1))) ;
error = (aPI - PI) * 100/PI;
//cout << "The value of pi through the leibniz formula is " << aPI << endl;
//cout << "The percent error from the calculated and actual value is " << error << endl;
}
cout << aPI << endl;
cout << error << endl;
cout << N << endl << endl << endl;
}
return 0;
}