# value of pi

• 09-26-2001
zalbag
value of pi
is there not something in C++ that has the preset value of pi? like in the math.h file?

Also does anyone remeber the formula for finding the value of a shpere and the area of a circle?
• 09-26-2001
Unregistered
Sphere V = (4/3)*R^3*PI
Circle S = R^2*PI
• 09-26-2001
Magos
Magos
While speaking of PI...

How do you calculate the actual value of PI? What algoritm do you use? It would be fun to make a program that calculates it (well, not with as many decimals as the super-computers do, but still...)
• 09-26-2001
alex
The preset value of pi should be in math.h, and is called M_PI. For some reason I don't know, the microsoft version of math.h does not include this constant. You can use pi=4*atan(1).

alex
• 09-26-2001
SOAK
#include <iostream.h>
#include <stdio.h>
#include <math.h>

typedef unsigned char byte;

int Drop();

int iDigits,*piChain;
byte *csDigits;

void main()
{
cout<<"Number of Decimals to Calculate:\n";
cin>>iDigits;
cout<<"\n";
++iDigits;
piChain=new int[iDigits*10/3+2];
csDigits=new byte[iDigits+1];
for(register int n=0;n<=iDigits*10/3+1;n++)
piChain[n]=2;

int lg=1;
int m;
for(n=0;n<iDigits;n++)
{
csDigits[n]=(byte)Drop();
if(csDigits[n]==9)
continue;
if(csDigits[n]==10)
{
for(m=lg;m<n;m++)
{
if(csDigits[m]!=9)
{
csDigits[m]=1+csDigits[m];
}
else
{
csDigits[m]=0;
}
}
csDigits[n]=0;
lg=n-1;
continue;
}
lg=n;
}
cout<<(int)csDigits[0]<<",\n";
for(n=1;n<iDigits;n++)
{
cout<<(int)csDigits[n];
}
cout<<"\n";
delete[] piChain;
delete[] csDigits;
}

int Drop()
{
int r,q,iDrop;
for(int m=0;m<=iDigits*10/3+1;m++)
piChain[m]=piChain[m]*10;
for(m=(iDigits*10/3+1);m>1;m--)
{
q=(int)floor(piChain[m]/(2*m-1));
r=piChain[m]%(2*m-1);
piChain[m]=r;
piChain[m-1]+=(q*(m-1));
}
iDrop=(int)floor(piChain[m]/10);
piChain[m]=piChain[m]%10;

return iDrop;
}