Fixed-point data and intermediate results

The compiler determines the number of integer and decimal places in an intermediate result.

Addition, subtraction, multiplication, and division

The following table shows the precision theoretically possible as the result of addition, subtraction, multiplication, or division.

Operation Integer places Decimal places
+ or - (i1 or i2) + 1, whichever is greater d1 or d2, whichever is greater
* i1 + i2 d1 + d2
/ i2 + d1 (d2 - d1) or dmax, whichever is greater

You must define the operands of any arithmetic statements with enough decimal places to obtain the accuracy you want in the final result.

The following table shows the number of places the compiler carries for fixed-point intermediate results of arithmetic operations that involve addition, subtraction, multiplication, or division in compatibility mode (that is, when the default compiler option ARITH(COMPAT) is in effect):

Value of i + d Value of d Value of i + dmax Number of places carried for ir
<30 or =30 Any value Any value i integer and d decimal places
>30 <dmax or =dmax Any value 30-d integer and d decimal places
>dmax <30 or =30 i integer and 30-i decimal places
>30 30-dmax integer and dmax decimal places

The following table shows the number of places the compiler carries for fixed-point intermediate results of arithmetic operations that involve addition, subtraction, multiplication, or division in extended mode (that is, when the compiler option ARITH(EXTEND) is in effect):

Value of i + d Value of d Value of i + dmax Number of places carried for ir
<31 or =31 Any value Any value i integer and d decimal places
>31 <dmax or =dmax Any value 31-d integer and d decimal places
>dmax <31 or =31 i integer and 31-i decimal places
>31 31-dmax integer and dmax decimal places

Exponentiation

Exponentiation is represented by the expression op1 ** op2. Based on the characteristics of op2, the compiler handles exponentiation of fixed-point numbers in one of three ways:

  • When op2 is expressed with decimals, floating-point instructions are used.
  • When op2 is an integral literal or constant, the value d is computed as
    d = d1 * |op2|
    

    and the value i is computed based on the characteristics of op1:

    • When op1 is a data-name or variable,
      i = i1 * |op2|
      
    • When op1 is a literal or constant, i is set equal to the number of integers in the value of op1 ** |op2|.

    In compatibility mode (compilation using ARITH(COMPAT)), the compiler having calculated i and d takes the action indicated in the table below to handle the intermediate results ir of the exponentiation.

    Value of i + d Other conditions Action taken
    <30 Any i integer and d decimal places are carried for ir.
    =30 op1 has an odd number of digits. i integer and d decimal places are carried for ir.
    op1 has an even number of digits. Same action as when op2 is an integral data-name or variable (shown below). Exception: for a 30-digit integer raised to the power of literal 1, i integer and d decimal places are carried for ir.
    >30 Any Same action as when op2 is an integral data-name or variable (shown below)

    In extended mode (compilation using ARITH(EXTEND)), the compiler having calculated i and d takes the action indicated in the table below to handle the intermediate results ir of the exponentiation.

    Value of i + d Other conditions Action taken
    <31 Any i integer and d decimal places are carried for ir.
    =31 or >31 Any Same action as when op2 is an integral data-name or variable (shown below). Exception: for a 31-digit integer raised to the power of literal 1, i integer and d decimal places are carried for ir.

    If op2 is negative, the value of 1 is then divided by the result produced by the preliminary computation. The values of i and d that are used are calculated following the division rules for fixed-point data already shown above.

  • When op2 is an integral data-name or variable, dmax decimal places and 30-dmax (compatibility mode) or 31-dmax (extended mode) integer places are used. op1 is multiplied by itself (|op2| - 1) times for nonzero op2.

    If op2 is equal to 0, the result is 1. Division-by-0 and exponentiation SIZE ERROR conditions apply.

Fixed-point exponents with more than nine significant digits are always truncated to nine digits. If the exponent is a literal or constant, an E-level compiler diagnostic message is issued; otherwise, an informational message is issued at run time.

Example: exponentiation in fixed-point arithmetic