Common Rules - COBOL

Subscript Evaluation

Unless otherwise specified by the rules for a specific statement, any subscripts that appear in an individual statement are evaluated only once as the first operation of the execution of that statement.

Arithmetic Statements

The arithmetic statements are ADD, COMPUTE, DIVIDE, MULTIPLY, and SUBTRACT. They have several features in common that are discussed in this section.

Modes of Operation

The data descriptions of the operands in an arithmetic statement need not be the same; any necessary mode conversion and decimal point alignment is supplied throughout the calculation.

Arithmetic operations are done in binary, packed decimal or unpacked decimal mode depending on the operation and on the usage of the operands.If the operation is division or exponentiation, it is done in unpacked decimal mode, first converting the values of one or both operands to that mode as necessary. When both operands of an addition or subtraction operation are binary, and they do not have the same number of positions to the right of the decimal point, the operation is done in unpacked decimal mode, first converting the values of both operands to that mode. Other operations are done in the higher mode of the two operands, with binary being treated as the lowest mode and unpacked decimal the highest. If the two operands are of the same mode the operation is done in that common mode; otherwise, the value of the operand having the lower mode is converted to the higher mode, and the operation is done using the converted value.

Composite Size

The composite size of specified operands in an arithmetic statement other than COMPUTE must not be greater than 30 digits. The specified operands in an ADD or SUBTRACT statement are those operands that contribute values to the final result; operands that serve only as receiving operands are not contributing operands. For example, in the statement ADD A B GIVING C, A and B are contributing operands, but C is not. In the statement ADD P TO Q, both P and Q are contributing operands. In the statement SUBTRACT X FROM Y GIVING Z, X and Y are contributing operands but Z is not.

The specified operands in a MULTIPLY or DIVIDE statement are all the receiving operands except for the operand of the REMAINDER phrase.

The composite size of a set of operands is the size that results when the operands are aligned on their decimal points and the maximum number of positions to the left of the common decimal point position is added to the maximum number of positions to the right of the common decimal point position. For example, if A is defined as PIC 9(8)V9(4) and B is defined as PIC 9(3)V9(6), the composite size of A and B is 8 + 6 = 14. The “phantom” positions resulting from the use of P in the PICTURE character-string are counted in determining the composite size. For example, if X is defined as PIC P(8)9(6) and Y is defined as PIC 9(8)P(10), the composite size of X and Y is 14 + 18 = 32, which exceeds the limit of 30.

ROUNDED Phrase

If, after decimal point alignment, the number of places in the fractional part of the result of an arithmetic operation is greater than the number of places provided for the fractional part of the resultant identifier, truncation is relative to the size provided for the resultant identifier. When the ROUNDED phrase is specified in the arithmetic statement, the absolute value of the resultant identifier is increased by one in the low-order digit position whenever the most significant digit of the excess is greater than or equal to five.

When the low-order integer positions in a resultant identifier are represented by the symbol P in the PICTURE character-string for that resultant identifier, rounding or truncation occurs relative to the rightmost integer position for which storage is allocated.

Size Error Condition

The size error condition occurs under any of the following circumstances:

• Violation of the rules for evaluation of exponentiation always terminates the arithmetic operation and always causes a size error condition.
• Division by zero always terminates the arithmetic operation and always causes a size error condition.
• If, after decimal point alignment and rounding (if specified), the absolute value of a result exceeds the largest value that can be contained in a resultant identifier, a size error condition exists. If the usage of a resultant identifier is binary, the largest value that can be contained in it is the maximum value implied by its PICTURE character-string. However, if a binary allocation override was specified that forced allocation of fewer bytes than needed to support the maximum value implied by its PICTURE character-string, then the maximum value is determined by the maximum value supported by the number of bytes specified in the binary allocation override and, for signed numbers the maximum for the absolute value of negative values is one greater than the maximum for positive numbers.For example, a one-byte signed binary data item can contain the values -128 to +127; the size error condition will exist on an attempt to store a value less than -128 or greater than +127 into a an item described as PIC S9(3) BINARY(1).

If the SIZE ERROR phrase is specified and a size error condition exists after the execution of the arithmetic operations specified by an arithmetic statement:

• The values of resultant identifiers for which a size error condition exists remain unchanged from the values they had before execution of the arithmetic statement.
• The values of resultant identifiers for which no size error condition exists are the same as they would have been if the size error condition had not resulted for any of the resultant identifiers.
• After completion of the arithmetic operations, control is transferred to imperative-statement-1 in the SIZE ERROR phrase and execution continues according to the rules for each statement specified in imperative-statement-1. If a procedure branching or conditional statement that causes explicit transfer of control is executed, control is transferred in accordance with the rules for that statement; otherwise, upon completion of execution of imperative-statement-1, control is transferred to the end of the arithmetic statement and the NOT SIZE ERROR phrase, if specified, is ignored.

If the SIZE ERROR phrase is not specified and a size error condition exists after the execution of the arithmetic operations specified by an arithmetic statement:

• The values of resultant identifiers for which a size error condition exists are undefined.
• The values of resultant identifiers for which no size error condition exists are the same as they would have been if the size error condition had not resulted for any of the resultant identifiers.
• After completion of the arithmetic operations, control is transferred to the end of the arithmetic statement and the NOT SIZE ERROR phrase, if specified, is ignored.

If the size error condition does not exist after the execution of the arithmetic operations specified by an arithmetic statement, the SIZE ERROR phrase, if specified, is ignored and control is transferred to the end of the arithmetic statement or to imperative-statement-2 in the NOT SIZE ERROR phrase if it is specified. In the latter case, execution continues according to the rules for each statement specified in imperative-statement-2. If a procedure branching or conditional statement that causes explicit transfer of control is executed, control is transferred in accordance with the rules for that statement; otherwise, upon completion of execution of imperative-statement-2, control is transferred to the end of the arithmetic statement.

For the ADD statement with the CORRESPONDING phrase and the SUBTRACT statement with the CORRESPONDING phrase, if any of the individual operations produces a size error condition, imperative-statement-1 in the SIZE ERROR phrase is not executed until all of the individual additions or subtractions are completed.

Overlapping Operands

When a sending and a receiving data item in any statement share a part or all of their storage areas, yet are not defined by the same data description entry, the result of the execution of such a statement is undefined. For statements in which the sending and receiving data items are defined by the same data description entry, the results of the execution of the statement may be defined or undefined depending on the general rules associated with the applicable statement. If there are no specific rules addressing such overlapping operands, the results are undefined.

In the case of reference modification, the unique data item produced by reference modification is not considered to be the same data description entry as any other data description entry. Therefore, if an overlapping situation exists, the results of the operation are undefined.

Incompatible Data

During the execution of the object program, the actual content of a data item is presumed to agree with the class of the data item as specified by its PICTURE clause. No checking is done by the runtime system to detect violations of this requirement, and results are undefined when violations occur. It is particularly important to ensure that the content of a data item described as numeric is in fact numeric when it is used in an arithmetic context.

This rule is suspended for a data item used as the operand of a class condition. Thus, in circumstances in which it is necessary to refer to a data item in an arithmetic context, and it is not certain that the content of the data item is compatible with that type of reference, an IF NUMERIC test should be applied.