2 * pi - various routines to calculate pi
4 * Copyright (C) 1999-2004 David I. Bell
6 * Calc is open software; you can redistribute it and/or modify it under
7 * the terms of the version 2.1 of the GNU Lesser General Public License
8 * as published by the Free Software Foundation.
10 * Calc is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
13 * Public License for more details.
15 * A copy of version 2.1 of the GNU Lesser General Public License is
16 * distributed with calc under the filename COPYING-LGPL. You should have
17 * received a copy with calc; if not, write to Free Software Foundation, Inc.
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * @(#) $Revision: 30.1 $
21 * @(#) $Id: pi.cal,v 30.1 2007/03/16 11:09:54 chongo Exp $
22 * @(#) $Source: /usr/local/src/bin/calc/cal/RCS/pi.cal,v $
24 * Under source code control: 1991/05/22 21:56:37
25 * File existed as early as: 1991
27 * Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/
31 * Calculate pi within the specified epsilon using the quartic convergence
38 local niter, yn, ym, tm, an, am, t, tn, sqrt2, epsilon2, count, digits;
43 digits = digits(1/epsilon);
44 if (digits <= 8) { niter = 1; epsilon = 1e-8; }
45 else if (digits <= 40) { niter = 2; epsilon = 1e-40; }
46 else if (digits <= 170) { niter = 3; epsilon = 1e-170; }
47 else if (digits <= 693) { niter = 4; epsilon = 1e-693; }
56 epsilon2 = epsilon/(digits/10 + 1);
57 digits = digits(1/epsilon2);
58 sqrt2 = sqrt(2, epsilon2);
59 bits = abs(ilog2(epsilon)) + 1;
60 bits2 = abs(ilog2(epsilon2)) + 1;
64 for (count = 0; count < niter; ++count) {
68 t = sqrt(sqrt(1-ym^4, epsilon2), epsilon2);
70 an = (1+yn)^4*am-tn*yn*(1+yn+yn^2);
71 yn = bround(yn, bits2);
72 an = bround(an, bits2);
74 return (bround(1/an, bits));
79 * Print digits of PI forever, neatly formatted, using calc.
81 * Written by Klaus Alexander Seistrup <klaus@seistrup.dk>
82 * on a dull Friday evening in November 1999.
84 * Inspired by an algorithm conceived by Lambert Meertens.
86 * See also the ABC Programmer's Handbook, by Geurts, Meertens & Pemberton,
87 * published by Prentice-Hall (UK) Ltd., 1990.
98 local a2, b2, p, q, d, d1;
99 local stdout = files(1);
100 local first = 1, row = -1, col = 0;
113 a1 = p * a2 + q * a1;
115 b1 = p * b2 + q * b1;
118 * Print common digits