home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / simtel / sigm / vols000 / vol023 / expo.pas < prev    next >
Pascal/Delphi Source File  |  1984-04-29  |  2KB  |  91 lines

  1. (*$I+,C-,S- ****************************************
  2. :
  3. :        EXPONENT PROGRAM
  4. :
  5. :    Ken Kuller donated this little program to the
  6. :  Pascal/Z Users Group in case someone else needed a
  7. :  benchmark program. 
  8. :
  9. ***************************************************)
  10.  
  11. Program Number_cruncher;
  12.  
  13. Const
  14.   zero      =        0.0;
  15.   epsilon   = 0.00000001;
  16.   one       =        1.0;
  17.  
  18.   initial   =      -10.0;       { First value in table }
  19.   increment =        0.5;       { Size of step         }
  20.   last      =       10.0;       { Last value in table  }
  21.  
  22.  
  23. Var
  24.   x, term   :       real;
  25.   expo      :       real;
  26.   n         :    integer;
  27.  
  28.  
  29. Function Series (last_term, X :    real;
  30.                  var n        : integer ) : real;
  31.   { This function calculates "e to the x" by evaluating the series,  }
  32.   { whose terms are  ( x**n ) / n!, and adding the them in order of  }
  33.   { increasing magnitude to reduce the truncation error.             }
  34.  
  35.   Var
  36.     term : real;
  37.  
  38.   Begin    { Series }
  39.     If x = zero
  40.       then
  41.         series := one
  42.       else    { x <> 0 }
  43.         if last_term < epsilon
  44.           then    { solution is close enough }
  45.             series := last_term
  46.           else
  47.             begin
  48.               n := n + 1;
  49.               term := last_term * x / n;
  50.               series := series ( term, x, n ) + last_term
  51.             end    { last_term > epsilon }
  52.   End;    { function series }
  53.  
  54.  
  55. Function Reciprocal ( { inverts } parameter : real ) : real;
  56.  
  57.   Begin
  58.     Reciprocal := one / parameter
  59.   End;    { Reciprocal }
  60.  
  61.  
  62. Function Exponent ( x : real ) : real;
  63.  
  64.   Begin
  65.     If x < zero
  66.       then
  67.         exponent := reciprocal ( exponent ( -x ) )
  68.       else
  69.         begin
  70.           n := 0;
  71.           term := one;
  72.           exponent := series ( term, x, n )
  73.         end    { else }
  74.   End;    { Exponent }
  75.  
  76.  
  77. Begin    { main program }
  78.  
  79.   { write table heading }
  80.   Writeln ( '           X      E to the X' );
  81.   Writeln ( '         =====  ============' );
  82.  
  83.   x := initial - increment;
  84.   repeat
  85.     x := x + increment;
  86.     expo := exponent(x);
  87.     writeln ( x :4 :1, expo :12 :6 )
  88.   until x = last
  89.  
  90. end.
  91.