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

With PTF for APAR PH58656 installed, full mode (that is, when the compiler option ARITH(FULL) is in effect) is also available. Full mode is the same as extended mode except for one difference: in full mode the intermediate result of a divide operation might contain a greater number of decimal digits. Specifically, ARITH(FULL) goes one step further than ARITH(EXTEND) in that after the normal rules have been followed for determining how many integer (i) and decimal (d) digits to retain in the intermediate result, if i + d < 31, d = 31 - i. In other words, ARITH(FULL) increases the number of decimal digits to retain with ARITH(EXTEND) up to the maximum it can within the 31 digit limit, and without reducing i, the number of integer digits to retain normally. This increased precision can end up changing the end result of the arithmetic expression being evaluated.

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)) or full mode (compilation using ARITH(FULL)), 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 or full 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