PDA

View Full Version : Calculate Pi

xcodeNewbie
Jul 12, 2011, 09:16 PM
I want to make an app that calculates pi to any decimal place, even 1,000,000. I don't care how long it takes the computer. I know the formula for pi is as follows:
pi / 4 = 4 * tan-1(1 / 5) - tan-1(1 / 239)
However, I am at a loss of how to do this.

jiminaus
Jul 12, 2011, 09:33 PM
You won't be able to use the standard C types such as double for this. You'll need an arbitrary-precision library like the GNU MP library http://gmplib.org/.

Have you search for code to do this? Surely it's been done before?

chown33
Jul 12, 2011, 11:54 PM
I want to make an app that calculates pi to any decimal place, even 1,000,000. I don't care how long it takes the computer. I know the formula for pi is as follows:
pi / 4 = 4 * tan-1(1 / 5) - tan-1(1 / 239)
However, I am at a loss of how to do this.

Using that formula, you'd first have to figure out how to calculate arctangents to arbitrarily long precision.

You might be better off using a series that converges to pi. Example google search terms:
pi series
pi convergent series

And the first step is probably to read the Wikipedia article on pi, which mentions series and even gives a simple example (Gregory series), which converges very slowly:
http://en.wikipedia.org/wiki/Pi

Also see:
http://en.wikipedia.org/wiki/Approximations_of_π

Loge
Jul 14, 2011, 05:09 PM
This problem is developed as an example in the following book, using C++ and the STL to create a class for arbitrary precision numbers.

http://www.amazon.com/STL-C-Programmers-Leen-Ammeraal/dp/0471971812/ref=sr_1_1?ie=UTF8&qid=1310680899&sr=8-1

only he uses the formula

pi = 48 * tan-1(1 / 18) + 32 * tan-1(1 / 57) - 20 * tan-1(1 / 239) which converges faster.