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 an internal intermediate result iir1. - Multiply iir1 by
A
yielding an internal intermediate result iir2. - Multiply iir2 by
A
yielding an internal intermediate result iir3. - Move iir3 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
.
Note: The internal intermediate results (iir1, iir2,
and iir3) obtained by the internal library routine
performing the exponential calculation in steps 1, 2, and 3 above
do not use the same decimal precision as ir4 and ir5 above.
Instead, those intermediate results are much more precise, ensuring
the most accurate result possible in ir4 or ir5.