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

  1. /*
  2. HEADER:        ;
  3. TITLE:        Series approximation of ln(x);
  4. VERSION:    1.0;
  5.  
  6. DESCRIPTION:    Function to perform a Taylor series approximation of the
  7.         function ln(x).  Large arguments are easily handled and
  8.         convergence is rapid;
  9.  
  10. KEYWORDS:    log(x), math functions, approximations;
  11. SYSTEM:        MSDOS;
  12. FILENAME:    LNX;
  13. WARNINGS:       Domain errors;
  14.  
  15. SEE-ALSO:    LNX, SINX, EXP, MATHCLUD.FUN, XPON;
  16. AUTHORS:    Dr. Ronald J. Terry;
  17. COMPILERS:    Turbo C, will also compile under QC, MS-C, power C;
  18. */
  19. /*  Natural logarithm of x */
  20. #include "mathclud.fun"
  21.  
  22. double lnx(double x)
  23. {
  24.      double sum=0,sum1=0,logtoln=2.30258509299404568402;
  25.      int i=1,basecount;
  26.      if(x<=0)
  27.      {
  28.      printf("Lnx is undefined at %lf\n",x);
  29.      return(-1e710);
  30.      }
  31. /* Caculate ln(x) as ln(x1) + ln(x2) where x = x1*x2 and x1 is a power of */
  32. /* 10 and ln(x1) = x1*ln(10) = x1*2.302... and x2 lies between 1 and 10   */
  33.      if(x<1)
  34.      {              /* Do if x is a fractional number smaller than 1 */
  35.        for(basecount=0;x<1;basecount--)
  36.      x = x*10.0;
  37.        if(x>3)
  38.        {
  39.      x = x/10.0;
  40.      basecount++;
  41.        }
  42.      }
  43.      else
  44.      {               /* Do if x is a number greater than or equal to 1 */
  45.        for(basecount=0;x>1;basecount++)
  46.      x = x/10.0;
  47.        if(x<=0.3)
  48.        {
  49.      x = x*10.0;
  50.      basecount--;
  51.        }
  52.      }
  53.      logtoln = logtoln*basecount;
  54.      x = (x-1)/(x+1);  /* Series expansion of lnx for x>0 */
  55.      for(;;)
  56.      {
  57.        sum = sum + Intpwr(x,i)/i;
  58.        i += 2;
  59.        if(Abs_val(sum - sum1)<=1e-15)
  60.      break;
  61.        sum1 = sum;
  62.      }
  63.      sum = 2*sum + logtoln;
  64.      return (sum);
  65. }