Chapter I - integer exponent

A frequently occuring operation in number-theoretic computations is raising a number to a power another number, also known as exponentiation. We would like an efficient way to compute X**Z (X to the Z power), where X is a real number and Z is a nonnegative integer. Usually we use the ** operator for calculation of the power function, raising a real number to a integer exponent. Another way to this calculation is the following algorithm POWER stated by al-Kashi about 1414 C.E.


Unit: internal function, external function without procedure statement
Parameters: real X, nonnegative integer Z
Returns: X**Z

POWER: procedure
parse arg X, Z; Pwr = 1
do forever
if Z // 2 then Pwr = Pwr * X
Z = Z % 2
if Z = 0 then return Pwr
X = X * X


[Regina] The following program ends with Error 26 ... line 3: Invalid whole number

/* BERNOULLI computes e */
numeric digits 20; Z = 1E+9
X = 1 + 1 / Z
say "e =" FORMAT(X**Z,,8)

But program

/* BERNOULLI computes e */
numeric digits 20; Z = 1E+9
X = 1 + 1 / Z
say "e =" FORMAT(POWER(X,Z),,8)
POWER: procedure

displays e = 2.71828183

Chapter II - real exponent

The following function REPOWER computes the result by raising the real number X to the given real exponent Z.


Unit: internal function
Parameters: real numbers X and Z, a positive integer P - number of significant digits of result, default is 9
Interface: the functions EXP and LN
Returns: X**Z or writes error message REAL_POWER: Result is undefined


REPOWER: procedure
parse arg X, Z, P
if P = "" then P = 9; P = P + 4; numeric digits P
if DATATYPE(Z, "Whole_number")
    if X = 0 & Z <= 0
      then call ERROR "REPOWER: Result is undefined"
      else return X ** Z
      when X > 0 then return EXP(Z * LN(X, P), P)
      when X = 0 & Z > 0 then return 0
        call ERROR "REPOWER: Result is undefined"
ERROR: say ARG(1); exit


Exponential function
Natural logarithms
Select computation of S = 0.5 * N**(2/3)
Technique: Bit array encoded as decimal number

Knuth D. E. Seminumerical Algorithms, vol. 2 of The Art of Computer Programming
Addison-Wesley, 1973

Cover Contents Index Main page Rexx page Mail

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