Exponential function

PROBLEM
Compute E**X where E is Euler's number e. E**X is EXP function. This function is defined:

EXP(X)=1+X/FACT(1)+X**2/FACT(2)+...+X**N/FACT(N)+...

where FACT is the factorial function and X is any number.

IMPLEMENTATION
Unit: internal function, external function without procedure statement
 
Parameters: a number X, a positive integer P - number of significant digits of EXP(X), default is 9
 
Interface: the ECONST function, for P<=200 or the E function for P>200
 
Returns: the first P significant decimal digits of EXP(X)
 


EXP: procedure
parse arg X, P
if P = "" then P = 9; numeric digits P
Y = X % 1
if ABS(X - Y) > 0.5 then Y = Y + SIGN(X)
X = X - Y
Sum = 1; Term = 1
do J = 1
  Term = Term * X / J; NewSum = Sum + Term
  if NewSum = Sum then leave
  Sum = NewSum
end
if Y = 0 then return Sum
return (ECONST() ** Y) * Sum

 

EXAMPLE
EXP(2.1879653,16) returns 8.917051119945531
VISUAL BASIC MsgBox(EXP(2.1879653)) displays 8.91705111994553

CONNECTIONS


Cover Contents Index Main page Rexx page   Mail

last modified 8th August 2001
Copyright © 2000-2001 Vladimir Zabrodsky
Czech Republic