pi - How to get the most decimal precision in C -
i calculating pi in program using indefinite series of terms. when display resulting calculation overall accuracy of pi not want. believe there problems in conversion specifications or primitive types using.
here getting:
pi: 3.141594
here want:
pi: 3.14159265358979323846
here code pi calculation method:
//global variables // variables hold number of threads , number of terms long numofthreads, numofterms; // variable store pieces of pi being calculated each thread double pitotal = 0.0; // use indefinite series of terms calulate pi void calculatepi(){ // variable store sign of each term double signofterm = 0.0; // variable to index of loop variable long k; #pragma omp parallel num_threads(numofthreads) \ default(none) reduction(+: pitotal) private(k, signofterm)\ shared(numofterms) (k = 0; k <= numofterms; k++) { if (k == 0) { signofterm = 1.0; } // sign of term else if (k % 2 == 0) { signofterm = 1.0; } // sign of term odd else if (k % 2 == 1) { signofterm = -1.0; } // computing pi using indefinite series of terms pitotal += (signofterm) * 4 / (2 * k + 1); } } // print result void printresult(){ printf("\n" "calulation of pi using %d " "terms: %f",numofterms,pitotal); }
here did solve problem. had change conversion specification %f %.17g. gave me more precision default print value of float.
// print result void printresult(){ //returning result 17 places after decimal removing trailing zeros printf("\n" "the value of pi using %d term(s): %.17g", numofterms, pitotal); }
Comments
Post a Comment