Arithmetic statement operands

The data descriptions of operands in an arithmetic statement need not be the same. Throughout the calculation, the compiler performs any necessary data conversion and decimal point alignment.

Size of operands

If the ARITH(COMPAT) compiler option is in effect, the maximum size of each operand is 18 decimal digits. If the ARITH(EXTEND) compiler option is in effect, the maximum size of each operand is 31 decimal digits.

The composite of operands is a hypothetical data item resulting from aligning the operands at the decimal point and then superimposing them on one another.

If the ARITH(COMPAT) compiler option is in effect, the composite of operands can be a maximum of 30 digits. If the ARITH(EXTEND) compiler option is in effect, the composite of operands can be a maximum of 31 digits.

The following table shows how the composite of operands is determined for arithmetic statements:

Table 1. How the composite of operands is determined
Statement Determination of the composite of operands
SUBTRACT
ADD
Superimposing all operands in a given statement except those following the word GIVING
MULTIPLY Superimposing all receiving data items
DIVIDE Superimposing all receiving data items except the REMAINDER data item
COMPUTE Restriction does not apply

For example, assume that each item is defined as follows in the DATA DIVISION:


A  PICTURE 9(7)V9(5).
B  PICTURE 9(11)V99.
C  PICTURE 9(12)V9(3).

If the following statement is executed, the composite of operands consists of 17 decimal digits:


ADD A B TO C

It has the following implicit description:


COMPOSITE-OF-OPERANDS PICTURE 9(12)V9(5).

In the ADD and SUBTRACT statements, if the composite of operands is 30 digits or less with the ARITH(COMPAT) compiler option, or 31 digits or less with the ARITH(EXTEND) compiler option, the compiler ensures that enough places are carried so that no significant digits are lost during execution.

In all arithmetic statements, it is important to define data with enough digits and decimal places to ensure the required accuracy in the final result. For more information, see Intermediate results and arithmetic precision in the Enterprise COBOL Programming Guide.

Overlapping operands

When operands in an arithmetic statement share part of their storage (that is, when the operands overlap), the result of the execution of such a statement is unpredictable.

Multiple results

When an arithmetic statement has multiple results, execution conceptually proceeds as follows:

  1. The statement performs all arithmetic operations to find the result to be placed in the receiving items, and stores that result in a temporary location.
  2. A sequence of statements transfers or combines the value of this temporary result with each single receiving field. The statements are considered to be written in the same left-to-right order in which the multiple results are listed.

For example, executing the following statement:


ADD A, B, C, TO C, D(C), E.

is equivalent to executing the following series of statements:


ADD A, B, C GIVING TEMP.
ADD TEMP TO C.
ADD TEMP TO D(C).
ADD TEMP TO E.

In the above example, TEMP is a compiler-supplied temporary result field. When the addition operation for D(C) is performed, the subscript C contains the new value of C.