Issue with my code not calculating pie π

Hello,

Sorry for my inexperience, i am in need of some help with my code.

My task at hand is to estimate π(Pi). The 2 required methods are the Euler series and the Gregory-Leibniz series

Rules for the code i have to use are.

• Add the statements to implement the gregory_leibniz and euler functions as described in
the program. These statements MUST be placed in the body of each function. Do not
make any changes to the main function other than adding the print statement
mentioned above.

• Do not declare any variables at the global level.
• Assume that all input values in the data file will be integers greater than zero

test file of values will have.

1
3
10000


Correct out put to show



Using 1 term(s)
Gregory-Leibniz:    4.000000000000000
Euler:              2.449489742783178              
Using 3 term(s)
Gregory-Leibniz:    3.466666666666667
Euler:              2.857738033247041
Using 10000 term(s)
Gregory-Leibniz:    3.141492653590034
Euler:              3.141497163947215




My broken out put.



Using 1 term(s)
Gregory-Leibniz:    3.141591653589774
Euler:                            inf
Using 3 term(s)
Gregory-Leibniz:    3.141591653589774
Euler:                            inf
Using 10000 term(s)
Gregory-Leibniz:    3.141591653589774
Euler:                            inf




My code that is not working.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78

//The Gregory-Leibniz and Euler infinite series for estimating pi
//will be implemented as value-returning functions. The program will
//read several integers from a file that represent the number of 
//terms to be used. For each data set, the program will display the
//number of terms and both estimated values with labels.

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
const int L = 20;  //field width of labels
const int N = 17;  //field width of numbers
double gregory_leibniz(int);
double euler(int);

int main()
{
  int terms;            //number of terms to use in estimates
  double leibniz_est;   //estimated value of pi using gregory-leibniz
  double euler_est;     //estimated value of pi using euler
  cout << fixed << setprecision(15);
  cin >> terms;
  while (cin)
    {
      cout << "Using " << terms << " term(s)" << endl;
      leibniz_est = gregory_leibniz(terms);
      euler_est = euler(terms);
      cout << setw(L) << left << "Gregory-Leibniz: ";
      cout << right << setw(N) << leibniz_est << endl;
      cout << setw(L) << left << "Euler: " << right
	   << setw(N) << euler_est << endl;
      cin >> terms;
	}
  return 0;
}
double gregory_leibniz(int numofterms)

{

numofterms = N;
    double pi = 0.0;
    for(int N = 1 ; N <=1000000; N *=10 )
    {     //calculate the results for N=1,10,...,1000000 
        pi = 0.0 ;                          
        for(int i= 1 ; i<=N ; ++i)
        {  
            int j = 2*i-1 ;                   //denominator term
            double o = 1.0/ j ; 
            o = (i%2 == 1)? o : -1*o ;        //Odd terms are subtracted, even terms are added
            pi += o ;
        }
        pi = 4*pi;
    }


}
double euler(int n)
//Given n terms, compute and return an estimate of pi
//using the Euler series.
{

double pi = 0.0;

for(int i = 0; i < n; ++i)
{
    pi += 1.0/(i*i);
}

pi *= 6;

pi = std::sqrt(pi);

}






closed account (DoLCX9L8)
In Euler's function you're missing a return statement, and also you're starting the for from 0 hence why you're getting inf (1/0)

In Leibniz's function you're also missing a return statement plus, your for N loop should be removed and you're not even using numofterms argument.

Also, it explicitly said you shouldn't declare variables at global level but you did so for L and N.
You might argue they're just constants but I'd argue they're constant variables.

I was able to recreate your output though.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <cmath>
#include <iostream>
#include <iomanip>
double euler(int n) 
{
	double pi = 0.0;
	for (int i=1; i<=n; ++i)
		pi += 1.0/(i*i);
	return std::sqrt(pi * 6);
}

double leibniz(int n) 
{
	double pi = 0.0;
	for (int i=1; i<=n; ++i)
		pi += (1.0 / (2 * i - 1)) * (i % 2 ? 1 : -1);
	return 4*pi;
}
int main() {
	int const L = 20, N = 17;
	int const test[] = {1, 3, 10000};
	std::cout << std::fixed << std::setprecision(15);
	for (int i=0; i<sizeof(test)/sizeof(int); ++i) {
		std::cout << "Using " << test[i] << " terms" << std::endl;
		std::cout << std::setw(L) << std::left << "Gregory-Leibniz: ";
		std::cout << std::right << std::setw(N) 
			<< leibniz(test[i]) << std::endl;
		std::cout << std::setw(L) << std::left << "Euler: ";
		std::cout << std::right << std::setw(N) 
			<< euler(test[i]) << std::endl;
	}

}
/*
Using 1 terms
Gregory-Leibniz:    4.000000000000000
Euler:              2.449489742783178
Using 3 terms
Gregory-Leibniz:    3.466666666666667
Euler:              2.857738033247041
Using 10000 terms
Gregory-Leibniz:    3.141492653590034
Euler:              3.141497163947215
*/

Last edited on
as a side note, 1000 terms not getting even 5 digits is pretty weak -- I think you could get better than that by counting the pixels in a big circle and dividing.

You might also be able to back it out via trig, but that is probably cheating :)
Last edited on
Topic archived. No new replies allowed.