# Calculate Pi

Discussion in 'iOS Programming' started by xcodeNewbie, Jul 12, 2011.

1. ### xcodeNewbie macrumors member

Joined:
Jul 1, 2011
#1
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.

2. ### jiminaus macrumors 65816

Joined:
Dec 16, 2010
Location:
Sydney
#2
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?

3. Jul 12, 2011
Last edited: Jul 12, 2011

### chown33 macrumors 604

Joined:
Aug 9, 2009
#3
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_π

Joined:
Jun 24, 2004
Location:
England
#4