Compaq COBOL AAQ2G1FTK User Manual

Page 108

Advertising
background image

Handling Numeric Data
2.7 Using the Arithmetic Statements

The SIZE ERROR phrase cannot be used with numeric MOVE statements. Thus,
if a program moves a numeric quantity to a smaller numeric item, it can lose
high-order digits. For example, consider the following move of an item to a
smaller item:

01 AMOUNT-A PIC S9(8)V99.
01 AMOUNT-B PIC S9(4)V99.

.
.
.
MOVE AMOUNT-A TO AMOUNT-B.

This MOVE operation always loses four of AMOUNT-A’s high-order digits.
The statement can be tailored in one of three ways, as shown in the following
example, to determine whether these digits are zero or nonzero:

1.

IF AMOUNT-A NOT > 9999.99

MOVE AMOUNT-A TO AMOUNT-B
ELSE ...

2.

ADD ZERO AMOUNT-A GIVING AMOUNT-B

ON SIZE ERROR ...

3.

COMPUTE AMOUNT-B = AMOUNT-A

ON SIZE ERROR ...

All three alternatives allow the MOVE operation to occur only if AMOUNT-A
loses no significant digits. If the value in AMOUNT-A is too large, all three avoid
altering AMOUNT-B and take the alternate execution path.

You can also use a NOT ON SIZE ERROR phrase to branch to, or perform,
sections of code only when no size error occurs.

2.7.6 Using the GIVING Phrase

The GIVING phrase moves the intermediate result of an arithmetic operation
to a receiving item. The phrase acts exactly like a MOVE statement in which
the intermediate result serves as the sending item, and the data item following
the word GIVING serves as the receiving item. When a statement contains a
GIVING phrase, you can have a numeric-edited receiving item.

The receiving item can also have the ROUNDED phrase. If the receiving item is
also numeric-edited, rounding takes place before the editing.

The GIVING phrase can be used with the ADD, SUBTRACT, MULTIPLY, and
DIVIDE statements. For example:

ADD A,B GIVING C.

2.7.7 Multiple Operands in ADD and SUBTRACT Statements

Both the ADD and SUBTRACT statements can contain a series of operands
preceding the word TO, FROM, or GIVING.

If there are multiple operands in either of these statements, the operands are
added together. The intermediate result of that operation becomes a single
operand to be added to or subtracted from the receiving item. In the following
examples, TEMP is an intermediate result item:

1.

Statement:

ADD A,B,C,D, TO E,F,G,H.

2–14 Handling Numeric Data

Advertising