8

I am looking for a formula/algorithm to calculate PI~3.14 in a given precision.

The formula/algorithm must have only very basic arithmetic as

  • +: Addition
  • -: Subtraction
  • *: Multiplication
  • /: Divison

because I want to implement these operations in C++ and want to keep the implementation as simple as possible (no bignum library is allowed).

I have found that this formula for calculating Pi is pretty simple:

Pi/4 = 1 - 1/3 + 1/5 - 1/7 + ... = sum( (-1)^(k+1)/(2*k-1) , k=1..inf ) 

(note that (-1)^(k+1) can be implemented easily by above operators).

But the problem about this formula is the inability to specify the number of digits to calculate. In other words, there is no direct way to determine when to stop the calculation.

Maybe a workaround to this problem is calculating the difference between n-1th and nth calculated term and considering it as the current error.

Anyway, I am looking for a formula/algorithm that have these properties and also converges faster to Pi

11
  • 1
    there are dozens of formulas which you can try here, and most of them converge pretty fast en.wikipedia.org/wiki/Pi Commented Dec 19, 2010 at 18:43
  • @Gabi: but most of them contain non-basic operations like square root. i am looking for a formula based on the +,-,*, and /. Please, please read all of the question. Commented Dec 19, 2010 at 18:45
  • Here's another wikipedia source that should help: en.wikipedia.org/wiki/Numerical_approximations_of_%CF%80. (And many are sum formulas using basic operators) Commented Dec 19, 2010 at 18:48
  • possible duplicate of Fastest way to get value of pi Commented Dec 19, 2010 at 22:05
  • I don't see the connection between wanting only 'simple' arithmetic ops and avoiding a bignum library: the standard C library contains a lot more than those operations, and you're going to have trouble calculating arbitrary digits without a bignum library regardless of what you use. Commented Dec 19, 2010 at 22:45

3 Answers 3

7

Codepad link:

#include <iostream> #include <cmath> int main() { double p16 = 1, pi = 0, precision = 10; for(int k=0; k<=precision; k++) { pi += 1.0/p16 * (4.0/(8*k + 1) - 2.0/(8*k + 4) - 1.0/(8*k + 5) - 1.0/(8*k+6)); p16 *= 16; } std::cout<<std::setprecision(80)<<pi<<'\n'<<M_PI; } 

Output:

3.141592653589793115997963468544185161590576171875 3.141592653589793115997963468544185161590576171875 

This is actually the Bailey-Borwein-Plouffe formula, also taken from the link from wikipedia.

Sign up to request clarification or add additional context in comments.

5 Comments

This formula is well-known as one of the formulas that gives nth digit of Pi, without the need of calculating n-1 digits before it. Then, is this formula relatively efficient (in respect to the other formulas) to calculate all n digits?
@Isaac I didn't test all of them, but if after 11 steps it finds the first 15 digits, do you still need better performance? (all the 15 digits are correct)
11 step for 15 digits seems really, really good! compared to the given summation in the question which needs 300 terms to calculate 2 decimal points! Anyway, I wonder the ability of BBP formula to immediately calculate nth digit of Pi may have impact on the performance of calculating all digits of Pi to n.
@Isaac I'm not the man to say whether or not it could have an impact, but it seems it works very well, so IMO it's ok. Also, I updated the answer with C code and comparison with M_PI
nice answer by the way. (although it cannot calculate Pi for big n s).
3

In your original (slowly converging) example, the error term can be computed because this is an alternating series; see http://en.wikipedia.org/wiki/Alternating_series#Approximating_Sums

Essentially, the next uncomputed term is a bound on the error.

Comments

-2

You can just do the Taylor envelope of the arctan(1) and then you will get pi/4 just summing all the rest part. The taylor envelope of arctan(1)

http://en.wikipedia.org/wiki/Taylor_series

also you can use the euler formula with z=1 and then multiply the result by 4.

http://upload.wikimedia.org/math/2/7/9/279bed5a2ea3b80a71f5b22078090168.png

3 Comments

The arctan formula is just the equation OP started with (which converges extremely slowly, btw).
Not if you sum the remains. Summing all remains will give a complete value too, until you start with the #0
"remains" aren't a real thing. Are you talking about the remainder term of the Taylor series? I don't know what you mean by summing that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.