PI is a mathematical constant used in the calculation of circles and various other mathematical operations, there is argument over what the value of the constant PI ~= 3.142 this little snippet shows how the value is derived from a finite series.
double derive_pi(int max_samples) { // arbitary constant-> incremented // by 2 to be used as denomiater (odd) // in the number series double odd = 1; // the sum of each element of the series double sum = 0; // maximum elements to be included in series/sample int max = max_samples; // boolean to determine +/- in number series bool bSign = true; // approximated pi value to be assigned here double pi = 0; int counter = 1; do { // if true - we add the preceding value // in series - if false we subtract the // preceding value in the series if(bSign) pi += 4 / odd; else pi -= 4 / odd; bSign = !bSign; // we increment +2 to increment through all odd numbers // total odd numbers to be included == the max samples. odd+=2; counter++; // add our current value of pi to the sum sum += pi; } while ( counter < max ); return (sum / counter); } // To acutally print the result effectively you will need to use // // remembering k is the number of elements in the series to be // // taken into account printf("%.16f", derive_pi(int k) ); // eg. 9000 elements // the more the better the accuracy printf("%.16f", derive_pi(9000) );
