792

I was trying various methods to implement a program that gives the digits of pi sequentially. I tried the Taylor series method, but it proved to converge extremely slowly (when I compared my result with the online values after some time). Anyway, I am trying better algorithms.

So, while writing the program I got stuck on a problem, as with all algorithms: How do I know that the n digits that I've calculated are accurate?

15
  • 23
    more of a mathematical problem. good algorithms also give an estimate of the error.
    – example
    Jan 11, 2013 at 17:17
  • 38
    Compare against pi? Jan 11, 2013 at 17:46
  • 55
    @chris: "Literally everywhere"? Jan 11, 2013 at 18:28
  • 33
    I can check for you up to 3.141592653589793238462643383279502, beyond that, why do you need such a big number of digits? (That's something like atomic level accuracy with a circle the size of the universe.) Jan 11, 2013 at 22:07
  • 69
    Why don't you just divide by pi and check if the result is 1? (just kidding)
    – user541686
    Jan 18, 2013 at 4:26

6 Answers 6

1661

Since I'm the current world record holder for the most digits of pi, I'll add my two cents:

Unless you're actually setting a new world record, the common practice is just to verify the computed digits against the known values. So that's simple enough.

In fact, I have a webpage that lists snippets of digits for the purpose of verifying computations against them: http://www.numberworld.org/digits/Pi/


But when you get into world-record territory, there's nothing to compare against.

Historically, the standard approach for verifying that computed digits are correct is to recompute the digits using a second algorithm. So if either computation goes bad, the digits at the end won't match.

This does typically more than double the amount of time needed (since the second algorithm is usually slower). But it's the only way to verify the computed digits once you've wandered into the uncharted territory of never-before-computed digits and a new world record.


Back in the days where supercomputers were setting the records, two different AGM algorithms were commonly used:

These are both O(N log(N)^2) algorithms that were fairly easy to implement.

However, nowadays, things are a bit different. In the last three world records, instead of performing two computations, we performed only one computation using the fastest known formula (Chudnovsky Formula):

Enter image description here

This algorithm is much harder to implement, but it is a lot faster than the AGM algorithms.

Then we verify the binary digits using the BBP formulas for digit extraction.

Enter image description here

This formula allows you to compute arbitrary binary digits without computing all the digits before it. So it is used to verify the last few computed binary digits. Therefore it is much faster than a full computation.

The advantage of this is:

  1. Only one expensive computation is needed.

The disadvantage is:

  1. An implementation of the Bailey–Borwein–Plouffe (BBP) formula is needed.
  2. An additional step is needed to verify the radix conversion from binary to decimal.

I've glossed over some details of why verifying the last few digits implies that all the digits are correct. But it is easy to see this since any computation error will propagate to the last digits.


Now this last step (verifying the conversion) is actually fairly important. One of the previous world record holders actually called us out on this because, initially, I didn't give a sufficient description of how it worked.

So I've pulled this snippet from my blog:

N = # of decimal digits desired
p = 64-bit prime number

Enter image description here

Compute A using base 10 arithmetic and B using binary arithmetic.

Enter image description here

If A = B, then with "extremely high probability", the conversion is correct.


For further reading, see my blog post Pi - 5 Trillion Digits.

15
  • 16
    And to answer the other question about how to know when a specific algorithm has converged to N digits: This requires that you know the convergence behavior of the algorithm. The Taylor Series of ArcTan(1) is logarithmically converging. So you'd need an exponentially large number of terms to converge - in short, don't use it.
    – Mysticial
    Jan 11, 2013 at 18:46
  • 23
    Yes, Chudnovsky's formula converges at a steady 14.18 digits per term. So you can divide the total number of digits by that to get how many terms you need. (Exact value is: Log(151931373056000)/Log(10) = 14.181647462725477655...)
    – Mysticial
    Jan 11, 2013 at 19:26
  • 7
    @erikb85 Kinda. The BBP formula (to some extent) counts as a second algorithm. But by itself it isn't enough since it doesn't verify the conversion to base 10. The idea of using BBP + conversion check to eliminate the need for a second computation wasn't mine. It was first done by Fabrice Bellard in his 2009 world record. It was such a good idea that we did the same and improved upon it.
    – Mysticial
    Jan 13, 2013 at 22:39
  • 86
    @FunsukWangadu I can only speak for myself, but here it goes: I never actually cared about Pi itself. To me, it's just another number. The value isn't in the number itself or the 10 terabytes of useless digits, it's the methods that are used to achieve it. The centuries of math, and the decades of computer/programming research that contributed to this feat are applicable to many other fields and thus are MUCH more valuable than a hard drive of digits. To put it simply: Computing the digits of Pi is more of a sport.
    – Mysticial
    May 3, 2013 at 21:33
  • 8
    @Mystical, just stumbled on your Pi calculation site from another stackoverflow question and couldn't help but gawk and giggle at what you guys did. Loved the harddrive failures/earthquakes in the logs :) pure amazing!
    – Joe
    Nov 28, 2013 at 2:50
50

Undoubtedly, for your purposes (which I assume is just a programming exercise), the best thing is to check your results against any of the listings of the digits of pi on the web.

And how do we know that those values are correct? Well, I could say that there are computer-science-y ways to prove that an implementation of an algorithm is correct.

More pragmatically, if different people use different algorithms, and they all agree to (pick a number) a thousand (million, whatever) decimal places, that should give you a warm fuzzy feeling that they got it right.

Historically, William Shanks published pi to 707 decimal places in 1873. Poor guy, he made a mistake starting at the 528th decimal place.

Very interestingly, in 1995 an algorithm was published that had the property that would directly calculate the nth digit (base 16) of pi without having to calculate all the previous digits!

Finally, I hope your initial algorithm wasn't pi/4 = 1 - 1/3 + 1/5 - 1/7 + ... That may be the simplest to program, but it's also one of the slowest ways to do so. Check out the pi article on Wikipedia for faster approaches.

1
  • 7
    That last formula (Leibniz formula, iirc) actually alternates addition and subtraction.
    – Thomas
    Jan 12, 2013 at 7:33
21

You could use multiple approaches and see if they converge to the same answer. Or grab some from the 'net. The Chudnovsky algorithm is usually used as a very fast method of calculating pi. http://www.craig-wood.com/nick/articles/pi-chudnovsky/

2
  • Reduces the chances but I still cannot be sure with multiple approach solution, what if both are wrong. Checking on net doesn't hold validity, then why not take values off net itself. I am thinking about bbp which one is more suited ? Jan 11, 2013 at 17:47
  • 8
    @IshanSharma If the two algorithms are independent, than the chances that both computations are wrong with identical results is pretty much zero. If anything goes wrong in either computation, the final results won't match - so you know at least one of them is wrong.
    – Mysticial
    Jan 11, 2013 at 17:55
15

The Taylor series is one way to approximate pi. As noted it converges slowly.

The partial sums of the Taylor series can be shown to be within some multiplier of the next term away from the true value of pi.

Other means of approximating pi have similar ways to calculate the max error.

We know this because we can prove it mathematically.

1
  • 2
    Seconded. I think most of the answers here are just not putting nearly enough weight on the concept of mathematical proof. Whatever your program is for calculating digits of pi, it's never going to be any more convincing than the most convincing mathematical proof that your program's method does indeed calculate pi. Which suggests a different constraint on programs that pi calculate pi: that they ought to aim as much for understandability as performance and correctness. Jan 14, 2013 at 22:21
5

You could try computing sin(pi/2) (or cos(pi/2) for that matter) using the (fairly) quickly converging power series for sin and cos. (Even better: use various doubling formulas to compute nearer x=0 for faster convergence.)

BTW, better than using series for tan(x) is, with computing say cos(x) as a black box (e.g. you could use taylor series as above) is to do root finding via Newton. There certainly are better algorithms out there, but if you don't want to verify tons of digits this should suffice (and it's not that tricky to implement, and you only need a bit of calculus to understand why it works.)

4
  • 7
    I don't quite see how it would help spot that the 1000th digit is off by 1. You would need very precise values of sin(pi/2) wouldn't you ? Jan 13, 2013 at 19:12
  • I'm not sure what to say about the previous answer, unless it is a joke or something. sin(pi/2) = 1 cos(pi/2) = 0 So, I'd say those sure converge fast. Jan 20, 2013 at 5:42
  • 17
    I guess it's not obvious to everyone that evaluating sin(x) and cos(x) to high precision is in fact much more difficult than computing Pi itself.
    – Mysticial
    Jan 20, 2013 at 6:04
  • 2
    For obvious reasons, you shouldn't use sin(pi/2) for this. Better to instead use sin(pi/6) and make sure it comes out as exactly 1/2. Dec 19, 2016 at 20:55
1

There is an algorithm for digit-wise evaluation of arctan, just to answer the question, pi = 4 arctan 1 :)

1
  • Someone down voting this, probably didn't understand the task "digits of pi sequentially". Mar 25, 2021 at 14:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.