Can someone have a look at my code? I'm trying to approximate pi by using the formula pi = lim 4*(1-1/3+1/5-...+(-1)^n/2n+1). When I compile my code, it always gives me 4 no matter what n I input. Thanks.
int main ()
{
double pi = 0;
std::cout << std::setprecision(10) << std::fixed << std::showpoint;
long n;
std::cout << " Enter the value of n: " << "\n";
std::cin >> n;
for(long i = 0; i < n; i++)
{
if(i % 2 == 0)
{
pi = pi + 1 / (2*i + 1);
}
else
{
pi = pi - 1 / (2*i + 1);
}
}
pi = pi *4;
std::cout << " The " << n << "th approximation of Pi is " << pi << "\n";
}
#include <iostream>
#include <iomanip>
int main()
{
double pi {};
unsignedlong n {};
std::cout << std::setprecision(10) << std::fixed << std::showpoint;
std::cout << " Enter the value of n: " << "\n";
std::cin >> n;
for (unsignedlong i = 0; i < n; ++i)
if (i % 2 == 0)
pi += 1.0 / (2 * i + 1);
else
pi -= 1.0 / (2 * i + 1);
std::cout << " The " << n << "th approximation of Pi is " << pi * 4 << "\n";
}
Or without using a conditional,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
#include <iomanip>
int main()
{
double pi {};
unsignedlonglong n {};
constdouble sgn[2] {1.0, -1.0};
std::cout << std::setprecision(10) << std::fixed << std::showpoint;
std::cout << " Enter the value of n: " << "\n";
std::cin >> n;
for (unsignedlonglong i = 0; i < n; ++i)
pi += sgn[i % 2] / (2 * i + 1);
std::cout << " The " << n << "th approximation of Pi is " << pi * 4 << "\n";
}
Enter the value of n:
900000000
The 900000000th approximation of Pi is 3.1415926525
you can eliminate the if and fix the problem at one stroke:
1 2 3 4 5 6 7 8 9 10 11 12 13
double one{-1.0};
std::cout << std::setprecision(10) << std::fixed << std::showpoint;
long n;
std::cout << " Enter the value of n: " << "\n";
std::cin >> n;
for(long i = 0; i < n; i++)
{
one *= -1;
{
pi = pi + one / (2*i + 1);
}
}
You can always add up the terms in pairs - then you will never have to worry about the alternating signs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
usingnamespace std;
int main()
{
constdouble realPI = 3.14159265358979323846;
cout.precision( 12 );
int n;
cout << "Enter an even n: "; cin >> n;
double PI = 4.0;
for ( int i = 2; i <= n; i += 2 ) PI -= 1.0 / ( 0.5 * i * i - 0.125 );
cout << fixed << PI << " Error: " << scientific << PI - realPI << '\n';
}
Enter an even n: 1000000000
3.141592662604 Error: 9.013743085262e-09
Because of floating-point precision it's actually more accurate to sum in reverse:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
usingnamespace std;
int main()
{
constdouble realPI = 3.14159265358979323846;
cout.precision( 12 );
int n;
cout << "Enter an even n: "; cin >> n;
double PI = 0.0;
for ( int i = n; i > 0; i -= 2 ) PI -= 1.0 / ( 0.5 * i * i - 0.125 );
PI += 4.0;
cout << fixed << PI << " Error: " << scientific << PI - realPI << '\n';
}
Enter an even n: 1000000000
3.141592654590 Error: 1.000000082740e-09
But you get a better result by numerical integration of the original function that gave that series: