I am calculating Pi in my program by using an indefinite series of terms. When I display the resulting calculation the overall accuracy of Pi is not what I want. I believe there are problems in the conversion specifications or the primitive types I am using.
Here is what I am getting:
Pi: 3.141594
Here is what I want:
Pi: 3.14159265358979323846
Here is some code from my Pi Calculation Method:
//Global variables
// Variables to hold the number of threads and the number of terms
long numOfThreads, numOfTerms;
// Variable to store the pieces of Pi as it is being calculated by each thread
double piTotal = 0.0;
// Use an indefinite series of terms to calulate Pi
void calculatePi(){
// Variable to store the sign of each term
double signOfTerm = 0.0;
// Variable to to be the index of the loop variable
long k;
#pragma omp parallel for num_threads(numOfThreads) \
default(none) reduction(+: piTotal) private(k, signOfTerm)\
shared(numOfTerms)
for (k = 0; k <= numOfTerms; k++) {
if (k == 0) {
signOfTerm = 1.0;
}
// Sign of term is even
else if (k % 2 == 0) {
signOfTerm = 1.0;
}
// Sign of term is odd
else if (k % 2 == 1) {
signOfTerm = -1.0;
}
// Computing pi using an indefinite series of terms
piTotal += (signOfTerm) * 4 / (2 * k + 1);
}
}
// Print the result
void printResult(){
printf("\n" "Calulation of Pi using %d " "terms: %f",numOfTerms,piTotal);
}