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).

  1. Multiply A by A yielding an internal intermediate result iir1.
  2. Multiply iir1 by A yielding an internal intermediate result iir2.
  3. Multiply iir2 by A yielding an internal intermediate result iir3.
  4. Move iir3 to ir4.

    ir4 has dmax decimal places. Because B is positive, ir4 is moved to Y. If B were equal to -4, however, an additional fifth step would be performed:

  5. 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.