# Help with calculating pi (beginner)

• 02-27-2013
JMaxwell
Help with calculating pi (beginner)
I have only just begun programming and i am a bit stuck on a current logic problem. The program i made (which is very simple) for the purpose of calculating pi is based on the infinite alternating series:

4/1 - 4/3 + 4/5 - 4/7 …

I've already worked out the mathematics and it works, but it's very inefficient (since the input is the number of iterations). Obviously the series converges to pi as it goes towards infinity, but as there is no way to repeat a program an infinite number of times (without waiting an infinite amount of time) the technique i've employed is flawed. I was wondering if someone could give me a suggestion as to how i could make my program more efficient and get a more accurate reading of pi (without waiting a really long time for the answer).

Thank you.

The source code i have up until now is:

Code:

```#include <iostream> #include <cmath> using namespace std; int main() {   double pi, series, entry, number = 1, alternating_sign;   int count;   char ans; do   {   cout << "Entry:";     cin >> entry;     for (count = 0; count < entry; count ++)     {       alternating_sign = pow((-1.0), (count));       series += alternating_sign / (number);       number+=2;     }     cout.setf(ios::fixed);     cout.setf(ios::showpoint);     cout.precision(10);   pi = 4*series;   cout << "Pi equals " << pi << endl;   cout << "Try again?\n";   cin >> ans;   } while (ans == 'Y' || ans == 'y');   return 0; }```
• 02-27-2013
std10093
Try this one.

π²/6 = 1/1² + 1/2² + 1/3² + 1/4² + 1/5² + 1/6² + 1/7² + 1/8² + ...

:)
• 02-27-2013
std10093
I calculated the absolute error of that(where x' is the machine number)
x' = 3.141592645

x= 3.141592653 (actual value)

|ε| = |x - x'| = 0.000000008 = 0.08 * 10^(-7) < 0.5*10^(-7)
which tell us that the machine value is going to be accurate at 7 decimal
digits at the most.Here it is exact 7 digits.

Then i calculated the absolute relative error

|ρ| = |ε| / x = 0,025464790899483937645941521750815 * 10^(-7) =
0,025464790899483937645941521750815 * 10^(-9) < 5 * 10^(-9)
which says that at the most nine significant digits are going to be
accurate.Here 8 digits are accurate.
• 02-27-2013
JMaxwell
Thanks for the help, that really sounds much simpler :). But im still worried about the iterations. I mean, wouldn't it have the same pitfall as it is increasing towards infinity. But i guess that really would depend on how many decimal points you intended to calculate the "true value" of pi.
• 02-27-2013
fightmx
You need to initialize the variable "series" to 0.
• 02-27-2013
JMaxwell
to std10093
I figured that i would probably would have to do that. But instead of placing a value exact to 8 digits i thought of making a program where the user types in the measured accuracy. I wanted to be able to input, let's say, a 20 digit measure; but i guess that would probably take a long time as well :tongue:. Thanks again for your help i will try and make the changes to correct the program.

By the way, do you know of any online or literary resource where i could obtain some further information on creating algorithms. The current textbook i'm using doesn't really have a lot of information on the development of logical sequences which is obviously crucial to programming.
Thanks.
• 02-27-2013
JMaxwell
thanks fightmx, stupid error on my part. Although i guess i also aimed too high wanting to calculate any number of accuracy on my computer.
• 02-27-2013
std10093
Check out this formula then. Might help. By the way, you can see the code for the other formula here. If you google you may found more :)
• 02-27-2013
std10093
Quote:

Originally Posted by JMaxwell
to std10093
I figured that i would probably would have to do that. But instead of placing a value exact to 8 digits i thought of making a program where the user types in the measured accuracy. I wanted to be able to input, let's say, a 20 digit measure; but i guess that would probably take a long time as well :tongue:. Thanks again for your help i will try and make the changes to correct the program.

By the way, do you know of any online or literary resource where i could obtain some further information on creating algorithms. The current textbook i'm using doesn't really have a lot of information on the development of logical sequences which is obviously crucial to programming.
Thanks.

You can let him set the term to stop ;).

I am not sure of what you are asking for. A great book for giving you an understand and make you think like a scientist in algorithms is the one of Cormen's. Google for it :)
• 02-27-2013
JMaxwell
Thanks a lot! I've barely started and i'm already trying to do things for myself :tongue:. Being a musician for ten years i know what it means that practice is what it takes to progress in a given subject.