home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 150_01 / tran.c < prev    next >
Text File  |  1985-09-07  |  2KB  |  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. #define E 2.718281828
  19.  
  20. /* Square root of x */
  21. double root(x)
  22. double x;
  23. {
  24. double a, x1, x2=1;
  25. x1 = x;
  26. if (x > 0) {
  27. while (x2 > .00000001) {
  28.     a = x;
  29.     x = x1 / a + a;
  30.     x = x / 2;
  31.     if (x >= a) x2=x-a;
  32.     else x2=a-x;
  33.     }
  34. }
  35. else x=0;
  36. return (x);
  37. }
  38.  
  39. /* Logarithm of x */
  40. double log(x)
  41. double x;
  42. {
  43. double a=0, x1, x2=0, x3=1;
  44. int t;
  45.     if (x>0) {
  46.     x1 = x;
  47.     for (t=0; x1<.5; t--)
  48.         x1 *= E;
  49.     for (t=t; x1>1.5; t++)
  50.         x1 /= E;
  51.     a = t;
  52.     x1 = x1-1;
  53.     x  = -x1;
  54.     for (t=1; x3>.0000001; t++) {
  55.         x2 = x1/t;
  56.         a = a + x2;
  57.         x1 = x1 * x;
  58.         x3 = (x2>=0) ? x2 : -x2;
  59.     }
  60.     }
  61. return (a);
  62. }
  63.  
  64. /* Exponential of x */
  65. double exp(x)
  66. double x;
  67. {
  68. double a, x1, x2, x3, x4, x7=1;
  69. int x5, x6, t;
  70.     if (x5=(x<0)) x=-x;
  71.     t  = x;
  72.     x4 = 1;
  73.     x = x-t;
  74.     for (x6 = 1; x6 <= t; x6++)
  75.         x4 *= E;
  76.     x1=x;
  77.     x=1;
  78.     x2=x1;
  79.     x3=0;
  80.     for (x6=2; x7>.0000001; x6++) {
  81.         x=x+x2;
  82.         x2=x2*x1/x6;
  83.         x7 = (x2 >= 0) ? x2 : -x2;
  84.     }
  85.     x = x*x4;
  86.     return (x5 ? 1/x : x);
  87. }
  88.