Back to index

lightning-sunbird  0.9+nobinonly
Functions
pi.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <time.h>
#include "mpi.h"

Go to the source code of this file.

Functions

mp_err arctan (mp_digit mul, mp_digit x, mp_digit prec, mp_int *sum)
int main (int argc, char *argv[])

Function Documentation

mp_err arctan ( mp_digit  mul,
mp_digit  x,
mp_digit  prec,
mp_int sum 
)

Definition at line 125 of file pi.c.

{
  mp_int   t, v;
  mp_digit q = 1, rd;
  mp_err   res;
  int      sign = 1;

  prec += 3;  /* push inaccuracies off the end */

  mp_init(&t); mp_set(&t, 10);
  mp_init(&v); 
  if((res = mp_expt_d(&t, prec, &t)) != MP_OKAY ||  /* get 10^prec    */
     (res = mp_mul_d(&t, mul, &t)) != MP_OKAY ||    /* ... times mul  */
     (res = mp_mul_d(&t, x, &t)) != MP_OKAY)        /* ... times x    */
    goto CLEANUP;

  /*
    The extra multiplication by x in the above takes care of what
    would otherwise have to be a special case for 1 / x^1 during the
    first loop iteration.  A little sneaky, but effective.

    We compute arctan(1/x) by the formula:

         1     1       1       1
        - - ----- + ----- - ----- + ...
        x   3 x^3   5 x^5   7 x^7

    We multiply through by 'mul' beforehand, which gives us a couple
    more iterations and more precision
   */

  x *= x; /* works as long as x < sqrt(RADIX), which it is here */

  mp_zero(sum);

  do {
    if((res = mp_div_d(&t, x, &t, &rd)) != MP_OKAY)
      goto CLEANUP;

    if(sign < 0 && rd != 0)
      mp_add_d(&t, 1, &t);

    if((res = mp_div_d(&t, q, &v, &rd)) != MP_OKAY)
      goto CLEANUP;

    if(sign < 0 && rd != 0)
      mp_add_d(&v, 1, &v);

    if(sign > 0)
      res = mp_add(sum, &v, sum);
    else
      res = mp_sub(sum, &v, sum);

    if(res != MP_OKAY)
      goto CLEANUP;

    sign *= -1;
    q += 2;

  } while(mp_cmp_z(&t) != 0);

  /* Chop off inaccurate low-order digits */
  mp_div_d(sum, 1000, sum, NULL);

 CLEANUP:
  mp_clear(&v);
  mp_clear(&t);

  return res;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int main ( int  argc,
char *  argv[] 
)

Definition at line 59 of file pi.c.

{
  mp_err       res;
  mp_digit     ndigits;
  mp_int       sum1, sum2;
  clock_t      start, stop;
  int          out = 0;

  /* Make the user specify precision on the command line */
  if(argc < 2) {
    fprintf(stderr, "Usage: %s <num-digits>\n", argv[0]);
    return 1;
  }

  if((ndigits = abs(atoi(argv[1]))) == 0) {
    fprintf(stderr, "%s: you must request at least 1 digit\n", argv[0]);
    return 1;
  }

  start = clock();
  mp_init(&sum1); mp_init(&sum2);

  /* sum1 = 16 * arctan(1/5)  */
  if((res = arctan(16, 5, ndigits, &sum1)) != MP_OKAY) {
    fprintf(stderr, "%s: arctan: %s\n", argv[0], mp_strerror(res));
    out = 1; goto CLEANUP;
  }

  /* sum2 = 4 * arctan(1/239) */
  if((res = arctan(4, 239, ndigits, &sum2)) != MP_OKAY) {
    fprintf(stderr, "%s: arctan: %s\n", argv[0], mp_strerror(res));
    out = 1; goto CLEANUP;
  }

  /* pi = sum1 - sum2         */
  if((res = mp_sub(&sum1, &sum2, &sum1)) != MP_OKAY) {
    fprintf(stderr, "%s: mp_sub: %s\n", argv[0], mp_strerror(res));
    out = 1; goto CLEANUP;
  }
  stop = clock();

  /* Write the output in decimal */
  {
    char  *buf = malloc(mp_radix_size(&sum1, 10));

    if(buf == NULL) {
      fprintf(stderr, "%s: out of memory\n", argv[0]);
      out = 1; goto CLEANUP;
    }
    mp_todecimal(&sum1, buf);
    printf("%s\n", buf);
    free(buf);
  }

  fprintf(stderr, "Computation took %.2f sec.\n", 
         (double)(stop - start) / CLOCKS_PER_SEC);

 CLEANUP:
  mp_clear(&sum1);
  mp_clear(&sum2);

  return out;

}

Here is the call graph for this function: