home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Interactive Guide / c-cplusplus-interactive-guide.iso / c_ref / csource3 / 150_01 / tran.c < prev    next >
Encoding:
C/C++ Source or Header  |  1985-09-07  |  1.8 KB  |  88 lines

  1. /*
  2.        HEADER:  150.??;
  3.         TITLE:  Transcendental Function Library;
  4.   DESCRIPTION:  "C subroutines to compute square root, log (to base e), and
  5.                  exponential functions.  Iterates until 1E-7 difference.";
  6.      KEYWORDS:  Transcendental Functions, Exponential, Square Root, Log;
  7.        SYSTEM:  any with suitable C compiler;
  8.      FILENAME:  TRAN.C;
  9.      WARNINGS:  "1) User documentation not included. 
  10.                  2) Requires a C compiler with double precision real numbers.";
  11.      SEE-ALSO:  HP.C;
  12.     COMPILERS:  any C compiler with double prec. reals;
  13.    REFERENCES:  AUTHORS: unknown; TITLE: "TRAN.C";
  14.                 CITATION: "PC-SIG Disk 50 or 142" 
  15.        ENDREF;
  16. */
  17. /* Transcendental function library */
  18.  
  19. #define E 2.718281828
  20.  
  21.  
  22.  
  23. /* Square root of x */
  24.  
  25. double root(x)
  26.  
  27. double x;
  28.  
  29. {
  30.  
  31. double a, x1, x2=1;
  32.  
  33. x1 = x;
  34.  
  35. if (x > 0) {
  36.  
  37. while (x2 > .00000001) {
  38.  
  39.     a = x;
  40.  
  41.     x = x1 / a + a;
  42.  
  43.     x = x / 2;
  44.  
  45.     if (x >= a) x2=x-a;
  46.  
  47.     else x2=a-x;
  48.  
  49.     }
  50.  
  51. }
  52.  
  53. else x=0;
  54.  
  55. return (x);
  56.  
  57. }
  58.  
  59.  
  60.  
  61. /* Logarithm of x */
  62.  
  63. double log(x)
  64.  
  65. double x;
  66.  
  67. {
  68.  
  69. double a=0, x1, x2=0, x3=1;
  70.  
  71. int t;
  72.  
  73.     if (x>0) {
  74.  
  75.     x1 = x;
  76.  
  77.     for (t=0; x1<.5; t--)
  78.  
  79.         x1 *= E;
  80.  
  81.     for (t=t; x1>1.5; t++)
  82.  
  83.         x1 /= E;
  84.  
  85.     a = t;
  86.  
  87.     x1 = x1-1;
  88.  
  89.     x  = -x1;
  90.  
  91.     for (t=1; x3>.0000001; t++) {
  92.  
  93.         x2 = x1/t;
  94.  
  95.         a = a + x2;
  96.  
  97.         x1 = x1 * x;
  98.  
  99.         x3 = (x2>=0) ? x2 : -x2;
  100.  
  101.     }
  102.  
  103.     }
  104.  
  105. return (a);
  106.  
  107. }
  108.  
  109.  
  110.  
  111. /* Exponential of x */
  112.  
  113. double exp(x)
  114.  
  115. double x;
  116.  
  117. {
  118.  
  119. double a, x1, x2, x3, x4, x7=1;
  120.  
  121. int x5, x6, t;
  122.  
  123.     if (x5=(x<0)) x=-x;
  124.  
  125.     t  = x;
  126.  
  127.     x4 = 1;
  128.  
  129.     x = x-t;
  130.  
  131.     for (x6 = 1; x6 <= t; x6++)
  132.  
  133.         x4 *= E;
  134.  
  135.     x1=x;
  136.  
  137.     x=1;
  138.  
  139.     x2=x1;
  140.  
  141.     x3=0;
  142.  
  143.     for (x6=2; x7>.0000001; x6++) {
  144.  
  145.         x=x+x2;
  146.  
  147.         x2=x2*x1/x6;
  148.  
  149.         x7 = (x2 >= 0) ? x2 : -x2;
  150.  
  151.     }
  152.  
  153.     x = x*x4;
  154.  
  155.     return (x5 ? 1/x : x);
  156.  
  157. }
  158.  
  159.