I have forgotten
my Password

Or login with:

  • Facebookhttp://facebook.com/
  • Googlehttps://www.google.com/accounts/o8/id
  • Yahoohttps://me.yahoo.com
Index » Programming » C/C++ »

Help on c++ (how to round correctly the pi value)

Serpoolet\′s Photo
10 May 09, 12:51AM
(4 replies)
Help on c++ (how to round correctly the pi value)
Ok first I will post my problem, I will explain it and I post my code:

Pi is an irrational number, i.e. it cannot be written as a fraction. It's approximate value of Pi is 3.141592653589793. Below are five different series which can be used to approximate PI. You can find it HERE: http://i44.tinypic.com/2ujtmxw.gif

Problem:

The function , ask which series he/she wants to use and the precision (number of digits, up to 10). If after 10000 iterations the series doesn't find the value of Pi, it Should display an error message.

Which series do you want? ( Options 1-2-3-4-5)

2

How many digits of accuracy? (Options >=1 and <=10)

5

It will display either an error message or the value of Pi and The number of iterations necessary to obtain that value. In order to round a real number to n decimal places, you can use the following algorithm

78375 divided by 10 ^ 3 = 78,375

Here it's my code that we can find the results of each series, but we must know the iterations. (so on this calculation I change the iterations and I set 10000 that ask the problem)

Code of each series:

for (long int n = 1; n <= 10000; n++)   //for loop for PI SERIES 1 
	 {          // loop start
	  pi1 += (4*pow(-1.0, n))/(2*n - 1);    // Calculating pi  of first series  with for loop value of
p is pi1
	 }         // loop end
 
for (long int n= 1; n <= 10000; n++)    //for loop for PI SERIES 2
     {        //loop start
serieValue1+= pow(-1.0, n+1)/pow(n,2.0);     // calculationg pi of second series with for loop 
value of p is pi2
     }        // loop end
pi2=sqrt(12*serieValue1);
 
for (long int n = 1; n <= 10000; n++)    //for loop for PI SERIES 3
     {       // loop start
serieValue2+= pow(+1.0, n+1)/pow(n,2.0);     // Calculating pi of third series with for loop  value
of p is pi3
     }       // loop end
pi3=sqrt(6*serieValue2);
 
for (long int j=1,n=1; n,j <= 10000; j++,n++) //for loop for PI SERIES 4                        
     { // loop start
if (j>=2)     //if staytment to check the j counter
n++;           // n=n+1 if the j>=2
serieValue3+= pow(+1.0, n-1)/pow(n,2.0);   // Calculating pi of the 4trd series  with for loop value
of p is pi4
     }// loop end 
pi4=sqrt(8*serieValue3);
 
for (long int n = 1; n <= 10000; n++)  //for loop for PI SERIES 5                     
    {//loop start 
serieValue4+= pow(+1.0, n+1)/pow(n*2,2.0);  //Calculating pi five series with for loop  value of p
is pi5
    }// loop end
pi5=sqrt(24*serieValue4);

The first problem that I had it's do round step by step all the results , in the number that the user set.

Then the second problem it's to round the Pi ==3.141592653589793.. in the number that the user set.

And at the end we want to make a check for each result's and when the results(round) is == with the P (round ) print The iterations that the series is approximate on Pi else if doesn't find at first 10000 iterations print error message.

Here is un example, and I then I will post my code:

Which series do you want? ( Options 1-2-3-4-5)

2

How many digits of accuracy? (Options >=1 and <=10)

5

The series is approximate on 1000 iterations. pi==3.14159.

So my problem on this code is I print the ValuePi to see if it's correct, and it's correct But only only with 5 digits if I set > that 5 then doesn't identified, also in the Pi1 round I'm not sure that I take the correct results,

I can't understand why that ..

Thanks for any help!!!

MY CODE:
void menu4() //menu4() - Find the number of iterations to approximate Pi.
{
int counter=0,digits;
char series;
 
long double pi1=0,total=0;
long double result=0;
 
long float ValuePi1=31;
long float ValuePi2=314;
long float ValuePi3=3141;
long float ValuePi4=31415;
long float ValuePi5=314159;
long float ValuePi6=3141592;
long float ValuePi7=31415926;
long float ValuePi8=314159265;
long float ValuePi9=3141592653;
long float ValuePi10=31415926535;
 
system("cls"); // clear screen 
cout <<setw(68)<< "|--------------------------------------------------|" << endl;
cout <<setw(68)<< "|                    Choice 3.                     |" << endl;
cout <<setw(68)<< "|--------------------------------------------------|" << endl;
cout <<setw(68)<< "|                                                  |" << endl;
cout <<setw(68)<< "|                                                  |" << endl;
cout <<setw(68)<< "| Find the number of iterations to approximate Pi. |" << endl;
cout <<setw(68)<< "|                                                  |" << endl;//Menu4() - Find
the number of iterations to approximate Pi.
cout <<setw(68)<< "|                                                  |" << endl;
cout <<setw(68)<< "|--------------------------------------------------|" << endl;
cout <<setw(24)<< "|Series|"<<endl;
cout <<setw(24)<< "--------"<<endl;
cout << "1.  P= 4*(1-(1/3)+(1/5)-(1/7)+(1/9)-..."<<endl;
cout << "2.  P= sqrt(12*(1- (1/4)+(1/9)-(1/16)+(1/25)-...))"<<endl;
cout << "3.  P= sqrt(6*(1+ (1/4)+(1/9)+(1/16)+(1/25)+...)) "<<endl;        // menu of the series 
cout << "4.  P= sqrt(8*(1+ (1/9)+(1/25)+(1/49)+...      )) "<<endl;
cout << "5.  P= sqrt(24*((1/4)+(1/16)+(1/36)+(1/64)+...  ))"<<endl;
cout << "--------------------------------------------------"<<endl;
cout << " Which series? (1-5): ";
cin>>series;     // ask the series 
 
cout << " How many digits of accuracy? (<10): ";
cin>>digits;  // ask the number of digits 
 
if ((series !='1' && series !='2' && series!='3' && series !='4' && series!='5') || ( digits <1 || digits>10))
{  // if start
cout <<endl;
cout <<" Error Input....."<<endl;
cout <<" Their are Only 5 series...(1,2,3,4,or5)"<<endl;
cout <<" And you can add different digits number.. digits>=1 or digits <=10"<<endl;  
cout <<" The program will be terminated in 5 seconds..."<<endl;
Sleep(5000);    // stop the program in 5 seconds 
} // if end
 
else 
 
if (series=='1') 
{ // end of if SERIS 1 
 
switch (digits)
{ //star of switch 
case 1:  result = ValuePi1/pow(10.0,digits);break;
case 2:  result = ValuePi2/pow(10.0,digits);break;
case 3:  result = ValuePi3/pow(10.0,digits);break;
case 4:  result = ValuePi4/pow(10.0,digits);break;
case 5:  result = ValuePi5/pow(10.0,digits);break;
case 6:  result = ValuePi6/pow(10.0,digits);break;
case 7:  result = ValuePi7/pow(10.0,digits);break;
case 8:  result = ValuePi8/pow(10.0,digits);break;
case 9:  result = ValuePi9/pow(10.0,digits);break;
case 10: result = ValuePi10/pow(10.0,digits);break;
} //end of switch 
 
cout<<endl;
cout<<"           ________        "<<endl;
cout<<"          |Series 1|       "<<endl;
cout<<" --------------------------"<<endl;
cout<<" Pi Aproximation: "<<result<<endl;
cout<<" --------------------------"<<endl;
 
for (int long n=1; n<=1000; n++) 
{//star for loop
	 total= pi1/pow(10.0,digits);
	 pi1+= (4*pow(-1.0, n+1))/(2*n - 1);
     counter++;
 
}//loop end
	 if (pi1==result)
	 { //if start
	   cout<<"Series "<<series<<" is approximate on "<<counter<<" iterations"<<" Series "<<series<<" is
"<<pi1<<endl;
	   cin.get();
	 } // if end 
	 else 
		 cout<<"This series doesn't approximate on Value Π, Change series or digits of accuracy"<<endl;
	 cin.get();
 
}//end of if staytment series 1 
cin.get();
} //end of void()

will\′s Photo
10 May 09, 2:30PM
Hi, I've a couple of comments:
  • digits variable isn't used by the series approximation. You get exactly the same value regardless of digits.
  • In you final code you've used series 1; where as in the example usage you enter series 2.
  • Pasting in your previous code for series 2, I get: 3.141592644041437 this is accurate to 7 significant digits.
  • What is a 'long float', isn't that a 'double'. Either way gcc doesn't like it so I would avoid using them.

On a more general point. Why do I keep finding people trying to calculate PI ! There are a number of good algorithms out there that evaluate it to thousands of digits. In deed there are whole books writing out PI. I suggest a quick read of Numerical Recipes

One further comment
cout << PI
Will only output to 5 significant digits, you therefore need something like
cout << setprecision(16);
cout << PI
Serpoolet\′s Photo
11 May 09, 12:09AM
digits variable isn't used by the series approximation. You get exactly the same value regardless of digits.

YOU have 100% right it's my first year in programing sorry

Pasting in your previous code for series 2, I get: 3.141592644041437 this is accurate to 7 significant digits.

yes and i want to make my program to read ONLY the digits that the user set for example 2 3.14 ONLY

In you final code you've used series 1; where as in the example usage you enter series 2. sorry i didn't see that i used series 2 because is approximate on 1000 iterations with 5 digits

Why do I keep finding people trying to calculate PI !

good theory, ask the university about that not me :D

also with that
cout << setprecision(16);
cout << PI

i just cout the PI before that how i can't round it and check it.... ??? this is my whole problem if you cout the PI you don't have problem but it is not the same thing if you check it.......( it's has other numbers,digits)
john\′s Photo
11 May 09, 1:02PM
If all you need to do is round to a specified number of decimal places try: http://www.codecogs.com/d-ox/io/format/roundto.php
Serpoolet\′s Photo
11 May 09, 2:28PM
thank you very much guys... my problem is fixed...

here is the code for the first series.......

for (int long n=1; n<=10000 && flag!=1; n++)  //for loop for series 1 
{//star for loop series 1
 
	   pi1+= (4*pow(-1.0, n+1))/(2*n - 1);   //calculation series 1 
       pi12=ceil((pi1*pow( 10.0,digits ) )- 0.49  ) / pow( 10.0,digits ); //round the results 
	 if (pi12==result) //if check..... for the result
	 {//if check  start
	    flag=1;  // if pi12==result  flag== 1 
	 }//if check end 	  
	 else //else of the result
		 counter++; // if the program don't find the approximate series change iterations ( calculation of
iteratios)
 
}//loop end series 1 
        if (flag==1) // if check = 1 
	 {  // start if 
	   cout<<" Series "<<series<<" is approximate on "<<counter<<" iterations"<<" Series "<<series<<"
is "<<pi12<<endl; 
	   cin>>pi12; //cin the result to print 
	 } // if end 
	 else  // else of check
 
     cout<<" This series doesn't approximate on Value PI...."<<endl; //print error message....
	 cin>>pi;//cin the result to print 
 
}//end of if staytment series 1
Currently you need to be logged in to leave a message.