title: Bailey–Borwein–Plouffe formula implementation in C++?
body: <p><strong>EDIT</strong>: The requirement was vague and instead of  calculating the n-th digit of pi they just wanted pi to the n-th digit not going beyond floats limitation so the brute force way worked for the requirements.</p>
  
  <p>I need to calculate PI the the n-th digit and I wanted to try using the <a href="http://en.wikipedia.org/wiki/Bailey%E2%80%93Borwein%E2%80%93Plouffe_formula">BBP formula</a> but am having difficulties. The equation I typed up doesn't seem to be giving me PI correctly.</p>
  
  <pre><code>(1 / pow(16,n))((4 / (8 * n + 1)) - (2 / (8 * n + 4)) - (1 / (8 * n + 5)) - (1 / (8 * n + 6)))
  </code></pre>
  
  <p>I was successful with the brute force way of finding PI but that is only so accurate and finding the nth number is difficult.</p>
  
  <pre><code>(4 - (4/3) + (4/5) - (4/7)...)
  </code></pre>
  
  <p>I wanted to find out if anyone had a better idea of how to do this or maybe help with my BBP equation on what I messed up?</p>
  
  <p>Thank you,<br>
  LF4</p>
  
  <p>Functional but not very accurate until a few iterations in and then you have to disreguard the last few.</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  using namespace std;
  
  int main()
  {
      int loop_num = 0;
      cout &lt;&lt; "How many digits of pi do you want?: ";
      cin  &gt;&gt; loop_num;
  
      double my_pi = 4.0;
      bool add_check = false;
      int den = 3;
      for (int i = 0; i &lt; loop_num; i++)
      {
          if (add_check)
          {
              my_pi += (4.0/den);
              add_check = false;
              den += 2;
          }
          else
          {
              my_pi -= (4.0/den);
              add_check = true;
              den += 2;
          }
      }
      cout &lt;&lt; "Calculated PI is: " &lt;&lt; my_pi &lt;&lt; endl;
      system("pause");
  
      return 0;
  }
  </code></pre>
  
  <p>What I'm hoping would be a better program.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cmath&gt;
  
  using namespace std;
  
  const double PI_BASE = 16.0;
  
  int main()
  {
      int loop_num = 0;
      cout &lt;&lt; "How many digits of pi do you want?: ";
      cin  &gt;&gt; loop_num;
  
      double my_pi = 0.0;
      for (int i = 0; i &lt;= loop_num; i++)
      {
          my_pi += ( 1.0 / pow(PI_BASE,i) )( (4.0 / (8.0 * i + 1.0)) -
                                             (2.0 / (8.0 * i + 4.0)) -
                                             (1.0 / (8.0 * i + 5.0)) -
                                             (1.0 / (8.0 * i + 6.0)) );
      }
      cout &lt;&lt; "Calculated PI is: " &lt;&lt; my_pi &lt;&lt; endl;
      system("pause");
  
      return 0;
  }
  </code></pre>