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 
)
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: