-1

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);
      }                              
Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
  • The value of `numOfTerms` determines the **number of iterations** that you algorithm will execute, not the number of decimal digits!!! – barak manos Mar 28 '15 at 14:25
  • And by the way, there is really no need to throw that "pile of code" in our face. Most of it has nothing to do with the question at hand. Please post **only** the relevant part, and PLEASE try to post it in an ordered and clean manner (i.e., a bit of indentation wouldn't do any harm). – barak manos Mar 28 '15 at 14:29
  • Okay well I thought it would help on diagnosing my issue... You're answer seems irrelevant. Care to go into more detail – golferdrew32 Mar 28 '15 at 14:35
  • go into detail about something you think is irrelevant? oh, wow... – Karoly Horvath Mar 28 '15 at 14:38
  • 1
    Well, now that you've described it in such a blunt manner, I'll allow myself to be a little "less polite" (and honestly - with no purpose of being nasty, so please try to see it as a constructive comment). You're code looks pretty bad. As a matter of fact, it looks absolutely horrible. The indentation, although semantic, shows very little care-ness about the details of your code. Those `if/else` statements imply very a poor logical thinking. And again, as I've mentioned in the previous comment, `numOfTerms` does not determine the number of decimal digits in the output. – barak manos Mar 28 '15 at 14:44
  • And with regards to the problem that you're complaining about - it's just a matter of how many floating-point digits `printf` outputs **by default**. You could have simply tried `printf("%f\n",3.14159265358979323846);`, and you would have realized it yourself. – barak manos Mar 28 '15 at 14:45
  • 1
    @KarolyHorvath: Please let me know if you think that my response (the two comments above) goes beyond a reasonable one. – barak manos Mar 28 '15 at 14:50
  • @barakmanos Thanks for clarifying that. I now understand what you mean. Sorry if I offended you or something. Thanks for the help! – golferdrew32 Mar 28 '15 at 15:58
  • Read [What Every Programmer Should Know About Floating-Point Arithmetic](http://floating-point-gui.de/). – pmg Mar 28 '15 at 16:38
  • on most current systems double is not able to store such a large precision number like 3.14159265358979323846 – phuclv Mar 28 '15 at 16:42
  • 1
    @barakmanos: You're asking the wrong person. I tend to be quite blunt :) – Karoly Horvath Mar 28 '15 at 17:08
  • This may help [Printf width specifier to maintain precision of floating-point value](http://stackoverflow.com/q/16839658/2410359) – chux - Reinstate Monica Mar 28 '15 at 19:03

2 Answers2

0

Here is what I did to solve the problem. I had to change the conversion specification from %f to %.17g. Which gave me more precision than the default print value of float.

 // Print the result
 void printResult(){
      //Returning the result up to 17 places after the decimal removing trailing zeros 
      printf("\n" "The value of Pi using %d term(s): %.17g", numOfTerms, piTotal);
      }
0

Note that (k%2 == 0) will handle the case k == 0, so you don't need a special check for k == 0. Also that series converges very slowly, it's the first one in the following list of simple formulas for pi:

pi/4 = arctan(1)

pi/4 = arctan(1/2) + arctan(1/3)

pi/4 = 4 * arctan(1/5) - arctan(1/239)

pi/4 = 6 * arctan(1/8) + 2* arctan(1/57) + arctan(1/239)

rcgldr
  • 27,407
  • 3
  • 36
  • 61