estimating pi

I'm a substitue teacher and I need to know how to estimates the value of the mathematical pi using c++ so i can help the students with any question they might have.
the program should caculate it up to the nth term given by user. it should also display each kth caculation as given by the user.
There are 10000000000000000 mathematical formulas to calculate Pi, starting with sums and Taylor series, and ending with partial derivatives and triple integrals. Which formula would you like to use? Go to wikipedia, and get some formula, and show us the formula you'd like to use, and we can help you. And please, make a first try!
here is what i have so far

#include <iostream>
#include <cmath> // ** add

using namespace std;

int input(string x);



int main()
{

int pie, denom = 3, terms, steps, counter; // add counter

float newPi = 4.0;

bool condition = true;

string prompt;

steps = input( "Display Pi after how many steps? " );
terms = input( "How many terms? " );

cout << "Results:";
cout << endl << endl;

counter = 0; // set counter to 0
for ( int i = 1; i <=terms; i++ ) // ** i <= terms
{
if (condition)
{

newPi -= ( 4.0 / denom );

condition = false;

denom += 2;

}

else
{

newPi += ( 4.0 / denom );

condition = true;

denom += 2;
}


counter++; // here we increment the counter and
if (counter == steps) { // see if it's time for a display step
cout << newPi << endl;
counter=0;
}
}



cout << endl << endl;
("PAUSE");
return 0;
}



int input(string x)
{
int y;
do {
cout << x;
cin >> y;
if (y<=0) cout << "Error -- invalid input" << endl << endl;

} while ( y <= 0 );

return y;
}
Did you read what I wrote? seriously! What mathematical formula did you choose to estimate Pi?

Under this website (which is the first result you'd find if you google "estimate pi"):

http://www.jimloy.com/geometry/pi.htm

Many formulas. I chose this:


pi/4=1 - 1/3 + 1/5 - 1/7 + 1/9 - . . .

since it's the most straight forward one.

So now, let's go to the program. First, write the function that calculates a single element of that series as a function of an integer (and multiply the result by 4), and then write the loop that adds its values:

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

double PiElement(double i)
{
   double res = (1)/(2*i-1); //this is the general term
   //now the sign
   //static cast is to convert from double to integer. The operator of the modulus requires an integer number
   if(static_cast<long int>(i) % 2 == 0) //if even, return it positive
      return -4*res;
   else //if odd, return it negative
      return 4*res;
//remember, multiplying by 4 because the formula estimates pi/4
}

int main()
{
   long int terms = 1000;

   double sum = 0;
   for(long int i = 1; i <= terms; i++)
   {
      sum = sum + PiElement(static_cast<double>(i)); //static cast because you're converting from integer (which is i) to double (which is the function parameter)
   }
   cout<<"Pi estimated value is: " <<sum<<endl;
}


Is that enough for you??
Last edited on
How would you wite it using the infinate series?
There's nothing such as infinite series in real world, even in mathematics, you bow and accept the precision you need and not infinite precision, and that's why there's a whole science for errors, and that's how physics work!!! nothing in the world is infinitely accurate!!!

You may keep the function running till you get the precision you need, and that depends on the type of variables you use. If you're using double, it's approximately 10^-16. So you can make a loop, where you keep taking the difference between every result and the next one, and if the difference is smaller than the tolerance you need, stop the loop and that's your Pi with the accuracy you need!
Topic archived. No new replies allowed.