A question

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 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"; 
}


Thanks in advance.
Last edited on
Lines 12 and 17 are identical. I guess you wanted to do sth. different for odd values of i.
Lines 12 and 17 look identical. What is the point of the condition?
Sorry, I changed the typo.
In C++, integer division always gives an int result. Change 1 to 1.0 to do a double division.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <iomanip>

int main()
{
	double pi {};
	unsigned long n {};

	std::cout << std::setprecision(10) << std::fixed << std::showpoint;
	std::cout << " Enter the value of n: " << "\n";
	std::cin >> n;

	for (unsigned long 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 {};
	unsigned long long n {};
	const double 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 (unsigned long long 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

Last edited on
Lines 12 and 17: avoid integer division

1
2
// pi = pi + 1 / (2*i + 1);  // line 12
pi = pi + 1.0 / (2*i + 1);


and likewise for line 17
Thank everyone for helping.
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>
using namespace std;

int main()
{
   const double 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>
using namespace std;

int main()
{
   const double 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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

int main()
{
   const double realPI = 3.14159265358979323846;
   cout.precision( 12 );

   int n;
   cout << "Enter n: ";   cin >> n;

   double PI = 0.0;
   double dx = 1.0 / n;
   for ( int i = 0; i < n; i++ )
   {
      double x = ( i + 0.5 ) * dx;
      PI += 1.0 / ( 1.0 + x * x );
   }
   PI *= 4.0 * dx;
   cout << fixed << PI << "      Error: " << scientific << PI - realPI << '\n';
}


Enter n: 1000000000
3.141592653590      Error: 1.776356839400e-13


Enter n: 1000
3.141592736923      Error: 8.333332957022e-08
Last edited on
Topic archived. No new replies allowed.