home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 344_01 / exp.c < prev    next >
Text File  |  1990-02-21  |  3KB  |  71 lines

  1. /*
  2. HEADER:        ;
  3. TITLE:          Series approximation for the exponential function e^x;
  4. VERSION:    1.0;
  5.  
  6. DESCRIPTION:    Calculates the value of exp(x) using a Taylor series
  7.         expansion of e^x.  The function as written converges rapidly
  8.         and can handle large values for x (i.e. up to 709.7).  Also,
  9.         this routine demonstrates techniques for speeding up
  10.         convergence and for handling large arguments in the series
  11.         approximation;
  12.  
  13. KEYWORDS:       Exp(x), math functions, approximations;
  14. SYSTEM:        MSDOS;
  15. FILENAME:       EXP;
  16. WARNINGS:       Bounds limits exceeded;
  17.  
  18. SEE-ALSO:    EXP, SINX, LNX, MATHCLUD.FUN;
  19.  
  20. AUTHORS:    Dr. Ronald J. Terry;
  21. COMPILERS:    Turbo C, will also compile under QuickC, MS-C and PowerC;
  22. */
  23. /* This program demonstrates several numerical calculations including    */
  24. /* calculation of exp(x), x^y (y integer,x>0), N factorial and absolute  */
  25. /* value of x.  It should be noted that a function to calculate x^y for  */
  26. /* x>0 and y fractional or integer may be developed from the routines to */
  27. /* calculate exp(x) and lnx(x).                                          */
  28. /*************************************************************************/
  29.  
  30. #include "mathclud.fun"
  31. double Xpon(double);
  32.  
  33. double Exp(double earg)
  34. {
  35.      const double e=2.71828182845904523536;
  36.      double ex,extmp;
  37.      int exint,i,neg=0;
  38.      if(earg>709.7)
  39.      {
  40.        printf("Bounds limit exceeded");
  41.        return(-1);
  42.      }
  43.      if(earg<0)
  44.      {
  45.        earg = -earg;
  46.        neg = 1;     /* set neg flag if argument is negative */
  47.      }
  48.  /* separate argument into an integral part and a fractional part.  Then  */
  49.  /* calculate exp(x) as exp(x1)*exp(x2) where x = x1 + x2.                */
  50.      exint = (int)earg; extmp = earg - exint;
  51.      ex = Xpon(extmp);
  52.      ex = ex*Intpwr(e,exint);
  53.      if(neg)
  54.        ex = 1/ex;
  55.      return (ex);
  56. }
  57. double Xpon(double x) /* Calculate exp(x) where x is the fractional part */
  58. {                     /* of the original argument.  This insures rapid   */
  59.      double s1=0,s2=0;   /* convergence and prohibits exceeding bounds.  */
  60.      int i=0;            /* for purposes of speed, x may be a number in  */
  61.      if(!x)              /* the range 0 < x < 10.  Although the original */
  62.        return(1);        /* argument may be as large as 709.7            */
  63.      for(;;)
  64.      {
  65.        s1 = s1 + (Intpwr(x,i))/(Ifac(i));
  66.        if(Abs_val(s1-s2)<1.0e-9)
  67.      break;
  68.        s2 =s1; i++;
  69.      }
  70.      return(s1);
  71. }