Back to index

lightning-sunbird  0.9+nobinonly
pi.c
Go to the documentation of this file.
00001 /*
00002  * pi.c
00003  *
00004  * Compute pi to an arbitrary number of digits.  Uses Machin's formula,
00005  * like everyone else on the planet:
00006  * 
00007  *    pi = 16 * arctan(1/5) - 4 * arctan(1/239)
00008  *
00009  * This is pretty effective for up to a few thousand digits, but it
00010  * gets pretty slow after that.
00011  *
00012  * ***** BEGIN LICENSE BLOCK *****
00013  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00014  *
00015  * The contents of this file are subject to the Mozilla Public License Version
00016  * 1.1 (the "License"); you may not use this file except in compliance with
00017  * the License. You may obtain a copy of the License at
00018  * http://www.mozilla.org/MPL/
00019  *
00020  * Software distributed under the License is distributed on an "AS IS" basis,
00021  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00022  * for the specific language governing rights and limitations under the
00023  * License.
00024  *
00025  * The Original Code is the MPI Arbitrary Precision Integer Arithmetic library.
00026  *
00027  * The Initial Developer of the Original Code is
00028  * Michael J. Fromberger.
00029  * Portions created by the Initial Developer are Copyright (C) 1999
00030  * the Initial Developer. All Rights Reserved.
00031  *
00032  * Contributor(s):
00033  *
00034  * Alternatively, the contents of this file may be used under the terms of
00035  * either the GNU General Public License Version 2 or later (the "GPL"), or
00036  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00037  * in which case the provisions of the GPL or the LGPL are applicable instead
00038  * of those above. If you wish to allow use of your version of this file only
00039  * under the terms of either the GPL or the LGPL, and not to allow others to
00040  * use your version of this file under the terms of the MPL, indicate your
00041  * decision by deleting the provisions above and replace them with the notice
00042  * and other provisions required by the GPL or the LGPL. If you do not delete
00043  * the provisions above, a recipient may use your version of this file under
00044  * the terms of any one of the MPL, the GPL or the LGPL.
00045  *
00046  * ***** END LICENSE BLOCK ***** */
00047 /* $Id: pi.c,v 1.3 2004/04/27 23:04:37 gerv%gerv.net Exp $ */
00048 
00049 #include <stdio.h>
00050 #include <stdlib.h>
00051 #include <string.h>
00052 #include <limits.h>
00053 #include <time.h>
00054 
00055 #include "mpi.h"
00056 
00057 mp_err arctan(mp_digit mul, mp_digit x, mp_digit prec, mp_int *sum);
00058 
00059 int main(int argc, char *argv[])
00060 {
00061   mp_err       res;
00062   mp_digit     ndigits;
00063   mp_int       sum1, sum2;
00064   clock_t      start, stop;
00065   int          out = 0;
00066 
00067   /* Make the user specify precision on the command line */
00068   if(argc < 2) {
00069     fprintf(stderr, "Usage: %s <num-digits>\n", argv[0]);
00070     return 1;
00071   }
00072 
00073   if((ndigits = abs(atoi(argv[1]))) == 0) {
00074     fprintf(stderr, "%s: you must request at least 1 digit\n", argv[0]);
00075     return 1;
00076   }
00077 
00078   start = clock();
00079   mp_init(&sum1); mp_init(&sum2);
00080 
00081   /* sum1 = 16 * arctan(1/5)  */
00082   if((res = arctan(16, 5, ndigits, &sum1)) != MP_OKAY) {
00083     fprintf(stderr, "%s: arctan: %s\n", argv[0], mp_strerror(res));
00084     out = 1; goto CLEANUP;
00085   }
00086 
00087   /* sum2 = 4 * arctan(1/239) */
00088   if((res = arctan(4, 239, ndigits, &sum2)) != MP_OKAY) {
00089     fprintf(stderr, "%s: arctan: %s\n", argv[0], mp_strerror(res));
00090     out = 1; goto CLEANUP;
00091   }
00092 
00093   /* pi = sum1 - sum2         */
00094   if((res = mp_sub(&sum1, &sum2, &sum1)) != MP_OKAY) {
00095     fprintf(stderr, "%s: mp_sub: %s\n", argv[0], mp_strerror(res));
00096     out = 1; goto CLEANUP;
00097   }
00098   stop = clock();
00099 
00100   /* Write the output in decimal */
00101   {
00102     char  *buf = malloc(mp_radix_size(&sum1, 10));
00103 
00104     if(buf == NULL) {
00105       fprintf(stderr, "%s: out of memory\n", argv[0]);
00106       out = 1; goto CLEANUP;
00107     }
00108     mp_todecimal(&sum1, buf);
00109     printf("%s\n", buf);
00110     free(buf);
00111   }
00112 
00113   fprintf(stderr, "Computation took %.2f sec.\n", 
00114          (double)(stop - start) / CLOCKS_PER_SEC);
00115 
00116  CLEANUP:
00117   mp_clear(&sum1);
00118   mp_clear(&sum2);
00119 
00120   return out;
00121 
00122 }
00123 
00124 /* Compute sum := mul * arctan(1/x), to 'prec' digits of precision */
00125 mp_err arctan(mp_digit mul, mp_digit x, mp_digit prec, mp_int *sum)
00126 {
00127   mp_int   t, v;
00128   mp_digit q = 1, rd;
00129   mp_err   res;
00130   int      sign = 1;
00131 
00132   prec += 3;  /* push inaccuracies off the end */
00133 
00134   mp_init(&t); mp_set(&t, 10);
00135   mp_init(&v); 
00136   if((res = mp_expt_d(&t, prec, &t)) != MP_OKAY ||  /* get 10^prec    */
00137      (res = mp_mul_d(&t, mul, &t)) != MP_OKAY ||    /* ... times mul  */
00138      (res = mp_mul_d(&t, x, &t)) != MP_OKAY)        /* ... times x    */
00139     goto CLEANUP;
00140 
00141   /*
00142     The extra multiplication by x in the above takes care of what
00143     would otherwise have to be a special case for 1 / x^1 during the
00144     first loop iteration.  A little sneaky, but effective.
00145 
00146     We compute arctan(1/x) by the formula:
00147 
00148          1     1       1       1
00149         - - ----- + ----- - ----- + ...
00150         x   3 x^3   5 x^5   7 x^7
00151 
00152     We multiply through by 'mul' beforehand, which gives us a couple
00153     more iterations and more precision
00154    */
00155 
00156   x *= x; /* works as long as x < sqrt(RADIX), which it is here */
00157 
00158   mp_zero(sum);
00159 
00160   do {
00161     if((res = mp_div_d(&t, x, &t, &rd)) != MP_OKAY)
00162       goto CLEANUP;
00163 
00164     if(sign < 0 && rd != 0)
00165       mp_add_d(&t, 1, &t);
00166 
00167     if((res = mp_div_d(&t, q, &v, &rd)) != MP_OKAY)
00168       goto CLEANUP;
00169 
00170     if(sign < 0 && rd != 0)
00171       mp_add_d(&v, 1, &v);
00172 
00173     if(sign > 0)
00174       res = mp_add(sum, &v, sum);
00175     else
00176       res = mp_sub(sum, &v, sum);
00177 
00178     if(res != MP_OKAY)
00179       goto CLEANUP;
00180 
00181     sign *= -1;
00182     q += 2;
00183 
00184   } while(mp_cmp_z(&t) != 0);
00185 
00186   /* Chop off inaccurate low-order digits */
00187   mp_div_d(sum, 1000, sum, NULL);
00188 
00189  CLEANUP:
00190   mp_clear(&v);
00191   mp_clear(&t);
00192 
00193   return res;
00194 }
00195 
00196 /*------------------------------------------------------------------------*/
00197 /* HERE THERE BE DRAGONS                                                  */