Use MOVE, ADD, SUBTRACT, MULTIPLY and DIVIDE in COBOL
Use the COBOL verbs MOVE, ADD, SUBTRACT, MULTIPLY and DIVIDE, with the TO, FROM, BY, GIVING and REMAINDER forms, and see how truncation and padding work.

COBOL was built to read like English business instructions, and its arithmetic verbs show it: you ADD one thing TO another and SUBTRACT a number FROM a field. This post covers the five verbs you use in nearly every program, MOVE, ADD, SUBTRACT, MULTIPLY and DIVIDE, with the forms that matter, a complete program and the output of each statement worked out. It uses Enterprise COBOL for z/OS 6.4 and assumes you have read the post on PIC clauses, since the way each verb handles digits depends on the picture of the target. The COMPUTE statement, which handles whole formulas, has its own post.
MOVE copies data
MOVE source TO target copies the value of the source into the target, converting it to fit the target's picture. The rules depend on the types:
Alphanumeric to alphanumeric: the text is placed at the left, padded with spaces on the right if the target is longer and cut on the right if it is shorter.
Numeric to numeric: the value is aligned on the decimal point, padded with zeros where the target has more positions and cut on the high-order or low-order side where it has fewer.
Numeric to edited: the number is formatted by the edit symbols.
A literal can be the source:
MOVE 'ABC' TO WS-CODEorMOVE 25 TO WS-QTY.
You can move one source to several targets in one statement, as in MOVE ZERO TO WS-A WS-B WS-C. The figurative constants ZERO, SPACE and HIGH-VALUES fill fields of any length.
Truncation is silent. Moving 12345 to a PIC 99 field leaves 45, and moving 12.789 to a PIC 99V9 field leaves 12.7, because a plain MOVE never rounds.
ADD and SUBTRACT
The two common forms of ADD are:
ADD WS-B TO WS-A
ADD WS-B WS-C GIVING WS-TOTALThe first adds WS-B to WS-A and stores the sum in WS-A. With TO, the last item is both an operand and the result. The second form, with GIVING, adds the items before it and stores the sum in WS-TOTAL, which is not itself an operand. You can name several results after GIVING.
SUBTRACT reads in the less familiar order:
SUBTRACT 5 FROM WS-A
SUBTRACT WS-B WS-C FROM WS-D GIVING WS-RESULTThe first subtracts 5 from WS-A and stores the result in WS-A. The second adds WS-B and WS-C, subtracts that sum from WS-D and stores the result in WS-RESULT. Beginners often read SUBTRACT A FROM B backwards: the item after FROM is the one you subtract from.
MULTIPLY and DIVIDE
MULTIPLY WS-A BY WS-B
MULTIPLY WS-A BY WS-B GIVING WS-RESULT
DIVIDE WS-B INTO WS-A
DIVIDE WS-A BY WS-B GIVING WS-QUOT REMAINDER WS-REMMULTIPLY a BY b stores a * b in b, and with GIVING the product goes to the named field and b is untouched.
DIVIDE b INTO a divides a by b and stores the quotient in a. DIVIDE a BY b GIVING q divides a by b and stores the quotient in q. The REMAINDER phrase stores what is left over in a second field. The quotient is truncated to the size of its field, so with a field for whole numbers, the quotient is the integer part and the remainder is the rest.
A complete program
IDENTIFICATION DIVISION.
PROGRAM-ID. ARITH.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-A PIC 9(3) VALUE 100.
01 WS-B PIC 9(3) VALUE 7.
01 WS-RESULT PIC 9(4).
01 WS-QUOT PIC 9(3).
01 WS-REM PIC 9(3).
01 WS-SMALL PIC 99.
PROCEDURE DIVISION.
ADD WS-B TO WS-A
DISPLAY 'A AFTER ADD = ' WS-A
SUBTRACT 5 FROM WS-A
DISPLAY 'A AFTER SUBTRACT = ' WS-A
MULTIPLY WS-A BY WS-B GIVING WS-RESULT
DISPLAY 'RESULT = ' WS-RESULT
DIVIDE WS-A BY WS-B GIVING WS-QUOT REMAINDER WS-REM
DISPLAY 'QUOT=' WS-QUOT ' REM=' WS-REM
MOVE 12345 TO WS-SMALL
DISPLAY 'SMALL = ' WS-SMALL
GOBACK.A AFTER ADD = 107
A AFTER SUBTRACT = 102
RESULT = 0714
QUOT=014 REM=004
SMALL = 45Check the arithmetic: 100 plus 7 is 107, minus 5 is 102, times 7 is 714 (shown with four digits because WS-RESULT has four positions), and 102 divided by 7 is 14 with a remainder of 4, shown as 014 and 004 because the fields are three digits.
What happens when the result does not fit
If a sum is too big for the target, the high-order digits are lost. Adding 900 to a PIC 9(3) field that holds 150 gives 1050, which does not fit, and the field ends up as 050. The program continues without a message, so the total is wrong with no sign of it.
Two clauses help. ON SIZE ERROR runs your statements when the result would not fit, and leaves the target unchanged:
ADD 900 TO WS-A
ON SIZE ERROR
DISPLAY 'TOTAL TOO LARGE'
NOT ON SIZE ERROR
DISPLAY 'ADDED OK'
END-ADDROUNDED makes the result round rather than truncate its decimals: ADD WS-X TO WS-Y ROUNDED. Both clauses are covered in the post on COMPUTE, where they matter most. The END-ADD is a scope terminator, which closes the statement so that a following statement is not read as part of it. Most arithmetic verbs have one: END-SUBTRACT, END-MULTIPLY and END-DIVIDE.
Division by zero
A division by zero does not abend by default. If you code ON SIZE ERROR, it takes that path, since a zero divisor is a size error. Without the clause, the result is not defined, and Enterprise COBOL may end with a data exception or a divide exception depending on the compiler options. Always test the divisor first:
IF WS-B = ZERO
DISPLAY 'DIVISOR IS ZERO'
ELSE
DIVIDE WS-A BY WS-B GIVING WS-QUOT
END-IFChoosing between the verbs and COMPUTE
The five verbs read well for single steps with clear data names. A formula with several operations, such as a tax calculation, reads better in COMPUTE. Many programmers use the verbs for counters and totals, such as ADD 1 TO WS-COUNT, and COMPUTE for everything else.
Beware of the intermediate results too. In ADD A B GIVING C, the sum is calculated in full precision before it is stored in C, so the truncation applies only at the end.
The statements are defined in the Procedure Division chapter of the Enterprise COBOL for z/OS Language Reference.
Reading the rounding and truncation rules together
Three separate things can change a number on its way into a field, and it helps to keep them apart. The first is alignment: the decimal points of source and target are lined up before anything else happens, so a PIC 9(3)V99 value of 025.50 moved to a PIC 9(4)V9 field becomes 0025.5, with the last decimal digit cut off. The second is truncation, which drops digits that have no place in the target, on the left of the integer part and on the right of the decimals. The third is rounding, which only happens when you ask for it with ROUNDED, and which adds one to the last kept digit when the first dropped digit is 5 or more.
Work through a case. Divide 10 by 3 into a field PIC 9V99: the exact quotient 3.3333 is cut to 3.33. Add ROUNDED and the result is still 3.33, since the first dropped digit is 3. Divide 20 by 3 and the quotient 6.6666 is cut to 6.66, but ROUNDED gives 6.67. Rounding has a cost in clarity as well as in cycles, so it belongs in the statements where a business rule asks for it, not everywhere.
A short worked example: an order line
An order line has a quantity of 12 and a unit price of 4.75. A program computes the line total, then splits a delivery fee of 10.00 evenly over three lines. With the verbs from this post, it might MULTIPLY WS-QTY BY WS-PRICE GIVING WS-LINE-TOTAL, giving 57.00, and DIVIDE 10 BY 3 GIVING WS-FEE-SHARE REMAINDER WS-FEE-LEFT. If the share field holds whole units, the share is 3 and the remainder is 1, so one unit of the fee is left to assign to a line by a business rule. If the share field has two decimals, the share is 3.33 and the remainder is 0.01. Either way, the remainder is the part that a plain division loses, and REMAINDER is how you account for it so that the shares add up to the whole.
That habit, never losing a remainder, is what separates a money program from a toy. Whenever a division splits an amount, make sure the pieces sum to the original, and put the leftover somewhere deliberate.


