home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / s / snip1292.zip / PI.C < prev    next >
C/C++ Source or Header  |  1991-09-08  |  4KB  |  156 lines

  1. /*
  2. **  PI.C - Computes Pi to an arbitrary number of digits
  3. **
  4. **  Uses far arrays so may be compiled in any memory model
  5. */
  6.  
  7. #include<stdio.h>
  8. #include<stdlib.h>
  9.  
  10. #if defined(__ZTC__)
  11.  #include <dos.h>
  12.  #define FAR _far
  13.  #define Fcalloc farcalloc
  14.  #define Ffree farfree
  15.  #define Size_T unsigned long
  16. #elif defined(__TURBOC__)
  17.  #include <alloc.h>
  18.  #define FAR far
  19.  #define Fcalloc farcalloc
  20.  #define Ffree farfree
  21.  #define Size_T unsigned long
  22. #else /* assume MSC/QC */
  23.  #include <malloc.h>
  24.  #define FAR _far
  25.  #define Fcalloc _fcalloc
  26.  #define Ffree _ffree
  27.  #define Size_T size_t
  28. #endif
  29.  
  30. long kf, ks;
  31. long FAR *mf, FAR *ms;
  32. long cnt, n, temp, nd;
  33. long i;
  34. long col, col1;
  35. long loc, stor[21];
  36.  
  37. void shift(long FAR *l1, long FAR *l2, long lp, long lmod)
  38. {
  39.       long k;
  40.  
  41.       k = ((*l2) > 0 ? (*l2) / lmod: -(-(*l2) / lmod) - 1);
  42.       *l2 -= k * lmod;
  43.       *l1 += k * lp;
  44. }
  45.  
  46. void yprint(long m)
  47. {
  48.       if (cnt<n)
  49.       {
  50.             if (++col == 11)
  51.             {
  52.                   col = 1;
  53.                   if (++col1 == 6)
  54.                   {
  55.                         col1 = 0;
  56.                         printf("\n");
  57.                         printf("%4ld",m%10);
  58.                   }
  59.                   else  printf("%3ld",m%10);
  60.             }
  61.             else  printf("%ld",m);
  62.             cnt++;
  63.       }
  64. }
  65.  
  66. void xprint(long m)
  67. {
  68.       long ii, wk, wk1;
  69.  
  70.       if (m < 8)
  71.       {
  72.             for (ii = 1; ii <= loc; )
  73.                   yprint(stor[(int)(ii++)]);
  74.             loc = 0;
  75.       }
  76.       else
  77.       {
  78.             if (m > 9)
  79.             {
  80.                   wk = m / 10;
  81.                   m %= 10;
  82.                   for (wk1 = loc; wk1 >= 1; wk1--)
  83.                   {
  84.                         wk += stor[(int)wk1];
  85.                         stor[(int)wk1] = wk % 10;
  86.                         wk /= 10;
  87.                   }
  88.             }
  89.       }
  90.       stor[(int)(++loc)] = m;
  91. }
  92.  
  93. void memerr(int errno)
  94. {
  95.         printf("\a\nOut of memory error #%d\n", errno);
  96.         if (2 == errno)
  97.                 Ffree(mf);
  98.         _exit(2);
  99. }
  100.  
  101. int main(int argc, char *argv[])
  102. {
  103.       int i=0;
  104.       char *endp;
  105.  
  106.       stor[i++] = 0;
  107.       if (argc < 2)
  108.       {
  109.             puts("\aUsage: PI <number_of_digits>");
  110.             return(1);
  111.       }
  112.       n = strtol(argv[1], &endp, 10);
  113.       if (NULL == (mf = Fcalloc((Size_T)(n + 3L), (Size_T)sizeof(long))))
  114.             memerr(1);
  115.       if (NULL == (ms = Fcalloc((Size_T)(n + 3L), (Size_T)sizeof(long))))
  116.             memerr(2);
  117.       printf("\nApproximation of PI to %ld digits\n", (long)n);
  118.       cnt = 0;
  119.       kf = 25;
  120.       ks = 57121L;
  121.       mf[1] = 1L;
  122.       for (i = 2; i <= (int)n; i += 2)
  123.       {
  124.             mf[i] = -16L;
  125.             mf[i+1] = 16L;
  126.       }
  127.       for (i = 1; i <= (int)n; i += 2)
  128.       {
  129.             ms[i] = -4L;
  130.             ms[i+1] = 4L;
  131.       }
  132.       printf("\n 3.");
  133.       while (cnt < n)
  134.       {
  135.             for (i = 0; ++i <= (int)n - (int)cnt; )
  136.             {
  137.                   mf[i] *= 10L;
  138.                   ms[i] *= 10L;
  139.             }
  140.             for (i =(int)(n - cnt + 1); --i >= 2; )
  141.             {
  142.                   temp = 2 * i - 1;
  143.                   shift(&mf[i - 1], &mf[i], temp - 2, temp * kf);
  144.                   shift(&ms[i - 1], &ms[i], temp - 2, temp * ks);
  145.             }
  146.             nd = 0;
  147.             shift((long FAR *)&nd, &mf[1], 1L, 5L);
  148.             shift((long FAR *)&nd, &ms[1], 1L, 239L);
  149.             xprint(nd);
  150.       }
  151.       printf("\n\nCalculations Completed!\n");
  152.       Ffree(ms);
  153.       Ffree(mf);
  154.       return(0);
  155. }
  156.