home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / dos_ency / 18 / exp2.bas < prev    next >
Encoding:
BASIC Source File  |  1988-08-11  |  2.2 KB  |  57 lines

  1. '  EXP.BAS -- COMPUTE EXPONENTIAL WITH INFINITE SERIES
  2. '
  3. ' ***********************************************************************
  4. ' *                                                                     *
  5. ' *  EXP                                                                *
  6. ' *                                                                     *
  7. ' *  This routine computes EXP(x) using the following infinite series:  *
  8. ' *                                                                     *
  9. ' *                       x    x^2   x^3   x^4   x^5                    *
  10. ' *         EXP(x) = 1 + --- + --- + --- + --- + --- + ...              *
  11. ' *                       1!    2!    3!    4!    5!                    *
  12. ' *                                                                     *
  13. ' *                                                                     *
  14. ' *  The program requests a value for x and a value for the convergence *
  15. ' *  criterion, C.  The program will continue evaluating the terms of   *
  16. ' *  the series until the amount added with a term is less than C.      *
  17. ' *                                                                     *
  18. ' *  The result of the calculation and the number of terms required to  *
  19. ' *  converge are printed.  The program will repeat until an x of 0 is  *
  20. ' *  entered.                                                           *
  21. ' *                                                                     *
  22. ' ***********************************************************************
  23.  
  24. '
  25. '  INITIALIZE program variables
  26. '
  27. Initialize:
  28.         TERMS = 1
  29.         FACT = 1
  30.         DELTA = 1.E35
  31.         EX = 1
  32. '
  33. '  Input user data
  34. '
  35.         INPUT "Enter number:  "; X
  36.         IF X = 0 THEN END
  37.         INPUT "Enter convergence criterion (.0001 for 4 places):  "; C
  38.  
  39. '
  40. '  Compute exponential until difference of last 2 terms is < C
  41. '
  42.         WHILE DELTA > C
  43.             FACT = FACT * TERMS
  44.             DELTA = X^TERMS / FACT
  45.             EX = EX + DELTA
  46.             TERMS = TERMS + 1
  47.         WEND
  48.  
  49. '
  50. '  Display answer and number of terms required to converge
  51. '
  52.         PRINT EX
  53.         PRINT TERMS; "elements required to converge"
  54.         PRINT
  55.  
  56.         GOTO INITIALIZE
  57.