Example: exponentiation in fixed-point arithmetic
The following example shows how the compiler performs an exponentiation to a nonzero integer power as a succession of multiplications, storing intermediate results as needed.
COMPUTE Y = A ** B
If B
is equal to 4, the result is
computed as shown below. The values of i and d that
are used are calculated according to the multiplication rules for
fixed-point data and intermediate results (referred to below).
- Multiply
A
byA
yielding ir1. - Multiply ir1 by
A
yielding ir2. - Multiply ir2 by
A
yielding ir3. - Move ir3 to ir4.
ir4 has dmax decimal places. Because
B
is positive, ir4 is moved toY
. IfB
were equal to -4, however, an additional fifth step would be performed: - Divide ir4 into 1 yielding ir5.
ir5 has dmax decimal
places, and would then be moved to Y
.