/* Area under curves and analysis of dimensionality Defining PI in a new way. It reaches M_PI to 7 digits at 10^8 samples Doing Pi Add sqrt(1-x^2) from 0 to 1 divide by steps Doing volume of a sphere also */ /** @name="MakingPI" @version="2" @author="moteyalpha" */ #include <stdio.h> #include <math.h> int main(int argc, char *argv[]) { double s,s2,stepsize,MyPi,d; int i,i2; int steps; double fsteps=10000000.0; steps=10000000; stepsize=1/(fsteps); /* The area without PI */ MyPi=0.0; for (i=1;i<steps;i++) { s = sqrt(1-(stepsize*i)*(stepsize*i)); MyPi=MyPi+s; } d=MyPi/fsteps; printf ("My PI=%f .. M_PI=%f\n",4*d,M_PI); /* Now the cubic without PI */ fsteps=60000.0; /* about two minutes */ steps=(int)fsteps; stepsize=1/(fsteps); MyPi=0.0; for (i=1;i<steps;i++) { s = sqrt(1-(stepsize*i)*(stepsize*i));MyPi=MyPi+s; for (i2=2;i2<steps;i2++) { /* Avoid that nasty imaginary number */ if ((s*s-(stepsize*i2*s)*(stepsize*i2*s))>0.0) { s2 = sqrt(s*s-(stepsize*i2*s)*(stepsize*i2*s)); MyPi=MyPi+s2*s; } } } d=MyPi/(fsteps*fsteps); printf ("My sphere volume factor*3 is %f.\n",3*(8*d)/M_PI); return 0; }
Archimedes would have done it this way.
0 comments:
Post a Comment