Blame nss/lib/freebl/mpi/doc/pi.txt

Packit 40b132
This file describes how pi is computed by the program in 'pi.c' (see
Packit 40b132
the utils subdirectory).
Packit 40b132
Packit 40b132
Basically, we use Machin's formula, which is what everyone in the
Packit 40b132
world uses as a simple method for computing approximations to pi.
Packit 40b132
This works for up to a few thousand digits without too much effort.
Packit 40b132
Beyond that, though, it gets too slow.
Packit 40b132
Packit 40b132
Machin's formula states:
Packit 40b132
Packit 40b132
	 pi := 16 * arctan(1/5) - 4 * arctan(1/239)
Packit 40b132
Packit 40b132
We compute this in integer arithmetic by first multiplying everything
Packit 40b132
through by 10^d, where 'd' is the number of digits of pi we wanted to
Packit 40b132
compute.  It turns out, the last few digits will be wrong, but the
Packit 40b132
number that are wrong is usually very small (ordinarly only 2-3).
Packit 40b132
Having done this, we compute the arctan() function using the formula:
Packit 40b132
Packit 40b132
                       1      1       1       1       1     
Packit 40b132
       arctan(1/x) := --- - ----- + ----- - ----- + ----- - ...
Packit 40b132
                       x    3 x^3   5 x^5   7 x^7   9 x^9
Packit 40b132
Packit 40b132
This is done iteratively by computing the first term manually, and
Packit 40b132
then iteratively dividing x^2 and k, where k = 3, 5, 7, ... out of the
Packit 40b132
current figure.  This is then added to (or subtracted from) a running
Packit 40b132
sum, as appropriate.  The iteration continues until we overflow our
Packit 40b132
available precision and the current figure goes to zero under integer
Packit 40b132
division.  At that point, we're finished.
Packit 40b132
Packit 40b132
Actually, we get a couple extra bits of precision out of the fact that
Packit 40b132
we know we're computing y * arctan(1/x), by setting up the multiplier
Packit 40b132
as:
Packit 40b132
Packit 40b132
      y * 10^d
Packit 40b132
Packit 40b132
... instead of just 10^d.  There is also a bit of cleverness in how
Packit 40b132
the loop is constructed, to avoid special-casing the first term.
Packit 40b132
Check out the code for arctan() in 'pi.c', if you are interested in
Packit 40b132
seeing how it is set up.
Packit 40b132
Packit 40b132
Thanks to Jason P. for this algorithm, which I assembled from notes
Packit 40b132
and programs found on his cool "Pile of Pi Programs" page, at:
Packit 40b132
Packit 40b132
      http://www.isr.umd.edu/~jasonp/pipage.html
Packit 40b132
Packit 40b132
Thanks also to Henrik Johansson <Henrik.Johansson@Nexus.Comm.SE>, from
Packit 40b132
whose pi program I borrowed the clever idea of pre-multiplying by x in
Packit 40b132
order to avoid a special case on the loop iteration.
Packit 40b132
Packit 40b132
------------------------------------------------------------------
Packit 40b132
 This Source Code Form is subject to the terms of the Mozilla Public
Packit 40b132
 # License, v. 2.0. If a copy of the MPL was not distributed with this
Packit 40b132
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.