Power
The ** (power) operator raises a number to
a power, which may be positive, negative, or 0. The power
must be a whole number. (The second term in the operation must be
a whole number and is rounded to DIGITS digits, if necessary, as described
under Numbers Used Directly by REXX.) If negative, the absolute value of the
power is used, and then the result is inverted (divided into 1). For
calculating the power, the number is effectively multiplied by itself
for the number of times expressed by the power, and finally trailing
zeros are removed (as though the result were divided by 1).
In practice (see Note 1 for the reasons), the
power is calculated by the process of left-to-right binary reduction.
For a**n: n is converted to binary, and a temporary
accumulator is set to 1. If n = 0 the initial
calculation is complete. (Thus, a**0 = 1 for
all a, including 0**0.) Otherwise each bit (starting
at the first nonzero bit) is inspected from left to right. If the
current bit is 1, the accumulator is multiplied by a. If
all bits have now been inspected, the initial calculation is complete;
otherwise the accumulator is squared and the next bit is inspected
for multiplication. When the initial calculation is complete, the
temporary result is divided into 1 if the power was negative.
The multiplications and division are done under the arithmetic
operation rules, using a precision of DIGITS + L + 1 digits. L is
the length in digits of the integer part of the whole number n (that
is, excluding any decimal part, as though the built-in function TRUNC(n) had
been used). Finally, the result is rounded to NUMERIC DIGITS digits,
if necessary, and insignificant trailing zeros are removed.