calculating pi to requested accuracy?

Just getting blank screen, i feel something is out of order but not sure what.

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
  #include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
using namespace std;

int main()
{
	double acc;
	int i;
	double pi = 0;
	double pie = 3.14159265359;
	


cout<<"This program will calculate the value of pi to the desired accuracy"<<endl
	<<"Please input the desired accuracy. (i.e. .001, .0001 etc."<<endl;
	cin>>acc;
	

	do
	{

	for (i = 1; i<= 100000; i++)

		pi += 4*(1/i)/((2*i)+1);
	

	}

	while(abs(pie - pi) > acc);
	
	

	cout<<pi<<endl;
	system("Pause");
	return 0;
}
Nothing ever prints inside your while loop, so the fact that you're getting a blank prompt indicates that your while loop is not ending.

Try putting the cout<< pi << endl inside your while loop.

Second, note that ( 1 / i ) performs integer division, which will almost always make the return be 0 if i is greater than 1 due to truncation. You probably want (1.0 / i).
Last edited on
Putting the cout<<pi<<endl; there created an infinite loop.

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
int main()
{
	double acc;
	int i;
	double pi = 0;
	double pie = 3.14159265359;


cout<<"This program will calculate the value of pi to the desired accuracy"<<endl
	<<"Please input the desired accuracy. (i.e. .001, .0001 etc."<<endl;
	cin>>acc;
	system("Cls");
	

	do
	{

	for (i = 1; i<= 100000; i++)

		pi += 4*(1.0/i)/((2.0*i)+1);
	
	cout<<pi<<endl;

	}

	while(abs(pie - pi) > acc);
	


	cout<<pi<<endl;
	system("Pause");
	return 0;
}
fixed the infinite loop, but now im getting 2.4548 for every input of "acc". is my equation wrong? I feel it is but i don't know how to add in the 'switching' negative sign into the equation.
Instead of using acc as a double, I would suggest using it as an int then use the setprecision(acc) within the cout to do the precision the user wants. This function is in the iomanip file.
well, my exact assignment is to create a program that outputs the value of pi to the given accuracy (input as .001, .01, etc.) by using a do-while loop and the leibniz series.
closed account (48T7M4Gy)
Are you sure your formula for pi is right?

Liebniz says it is 4*Sum( -1^k/(2k+1) ) every second term is -ve while yours are all +ve.
Here is my current code, i am still not receiving anything from the for loop. When i put the cout inside the loop, it becomes infinite.

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
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
using namespace std;

int main()
{
	double acc;
	double i;
	double pi = 0;
	double pie = (16*(atan(1.0/5.0))-(4*atan(1.0/239)));


cout<<"This program will calculate the value of pi to the desired accuracy"<<endl
	<<"Please input the desired accuracy. (i.e. .001, .0001 etc."<<endl;
	cin>>acc;
	system("Cls");
	

	do
	{

	for (i = 1; i<= 100000; i++)

		pi +=4*pow(-1.0, i+1)/((2.0*i)+1);
	
	

	}

	while(abs(pie - pi) > acc);
	


	cout<<pi<<endl;
	system("Pause");
	return 0;
}
closed account (48T7M4Gy)
for (i = 0; i<= 100000; i++)


pi +=4*pow(-1.0, i)/((2.0*i)+1);
Last edited on
Having a cout statement can't make something be infinite.

Your loop is infinite whether or not you have a cout statement in there.

Your "pi" number is divergent and increases well past 3.14, so it's a problem with your line 26.

Also note that your for loop is kinda useless in this regard, since it's already wrapped around a loop. In fact it might be messing you up since it's resetting the value of i each time.
Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
	int i = 0;
	do
	{
	  pi +=4*pow(-1.0, i)/((2.0*i)+1);
	  i++;
	}
	while(abs(pie - pi) > acc || i < 100000);


This makes a bit more sense.
Still getting same problem, don't know where i am going wrong. Is it still my "pi"? Still not ever getting a value after i run the program. If i put the cout<<pi; inside my do-while, it outputs the infinite loop of it running the equation. How do i get the 'correct' value of pi out of the loop?

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
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
using namespace std;

int main()
{
	double acc;
	double i = 0;
	double pi = 0;
	double pie = (16*(atan(1.0/5.0))-(4*atan(1.0/239)));


cout<<"This program will calculate the value of pi to the desired accuracy"<<endl
	<<"Please input the desired accuracy. (i.e. .001, .0001 etc."<<endl;
	cin>>acc;
	system("Cls");
	

	do
	{

		pi +=4*(pow(-1.0, i+1)/((2.0*i)+1));
	
	i++;

	
	}

	while(abs(pie - pi) > acc);
	
	

	cout<<pi<<endl;

	system("Pause");
	return 0;
}

closed account (48T7M4Gy)
i+1 is wrong. You are starting at the second term of the series.
i is an int.
i < 100000.
the i < 100000 was arbitrary, I don't need/want it in there now. I just want it to stop when acc is at the desired.
I think i have it, but why is it always giving me 5 digits past the decimal? is that just default, because i haven't put in fixed, setw(), setprecision(), yet.

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
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
using namespace std;

int main()
{
	double acc;
	int i = 0;
	double pi = 0;
	double pie = (16*(atan(1.0/5.0))-(4*atan(1.0/239)));


cout<<"This program will calculate the value of pi to the desired accuracy"<<endl
	<<"Please input the desired accuracy. (i.e. .001, .0001 etc."<<endl;
	cin>>acc;
	system("Cls");
	

	do
	
	{
		pi +=4*(pow(-1.0, i)/((2.0*i)+1));
	
	i++;
	system("Cls");
	cout<<pi;
	
	
	}
	while(abs(pie - pi) > acc);
	
	

	

	system("Pause");
	return 0;
}
closed account (48T7M4Gy)
Give setprecision a whirl. Worst case: new computer.

cout << setprecision(4);

:-) cya
thanks!
Remember that double has an accuracy of around 18 decimal places (on PCs), so don't try to exceed that.
sorry, one last question. I put a loop in so the program could be repeated, it works fine with everything except when i put in a string that is not "yes" or "Yes", i.e. no, asdf, yea, etc. one character input exits the program, and the yes or Yes repeats, but everything else sends it into an infinite loop or something.
also, there are red lines under my 'system' 's, saying that it is ambiguous. It still compiles so I don't know what it is doing or not doing.

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<iostream>
#include<iomanip>
#include<cmath>
#include<string>
using namespace std;

int main()
{
	double acc;
	int i = 0;
	double pi = 0;
	double pie = (16*(atan(1.0/5.0))-(4*atan(1.0/239)));
	string ans = "yes";

	while("yes" == ans || ans == "Yes")

	{

cout<<"This program will calculate the value of pi to the desired accuracy"<<endl
	<<"Please input the desired accuracy. (i.e. .001, .0001 etc."<<endl;
	cin>>acc;
	system("Cls");
	

	do
	
	{
		pi +=4*(pow(-1.0, i)/((2.0*i)+1));
	
	i++;
	system("Cls");
	cout<<fixed<<setw(10)<<setprecision(6)<<pi;

	}
	while(abs(pie - pi) > acc);
	
	cout<<endl<<"Would you like to run this program again? Yes or yes for yes, anything else for no"<<endl;
	cin>>ans;
	cout<<endl;
	}
	
	system("Pause");
	return 0;
}
also, there are red lines under my 'system' 's, saying that it is ambiguous. It still compiles so I don't know what it is doing or not doing.

That's because you're missing
 
#include <cstdlib> 
Last edited on
Topic archived. No new replies allowed.