MULTIPLY and DIVIDE

MULTIPLY and DIVIDE follow the pattern of ADD, with one trap of their own: the dividend comes second in DIVIDE ... INTO and first in DIVIDE ... BY, and the two spellings read in opposite directions.

BY and INTO

Twelve items at five and a quarter, split seven ways, with the remainder kept, and a price that is doubled and then quartered:

IDENTIFICATION DIVISION. PROGRAM-ID. MULDIV. DATA DIVISION. WORKING-STORAGE SECTION. 01 QTY-F PIC 9(3) VALUE 12. 01 PRICE-F PIC 9(3)V99 VALUE 5.25. 01 TOTAL-F PIC 9(6)V99 VALUE 0. 01 PARTS-F PIC 9(3) VALUE 7. 01 EACH-F PIC 9(3)V99 VALUE 0. 01 QUOT-F PIC 9(3) VALUE 0. 01 REM-F PIC 9(2) VALUE 0. PROCEDURE DIVISION. MULTIPLY QTY-F BY PRICE-F GIVING TOTAL-F. DISPLAY "12 * 5.25 = [" TOTAL-F "]". DIVIDE PARTS-F INTO TOTAL-F GIVING EACH-F. DISPLAY "total / 7 = [" EACH-F "]". DIVIDE PARTS-F INTO 100 GIVING QUOT-F REMAINDER REM-F. DISPLAY "100 / 7 = [" QUOT-F "] rem [" REM-F "]". MULTIPLY 2 BY PRICE-F. DISPLAY "price doubled [" PRICE-F "]". DIVIDE 4 INTO PRICE-F. DISPLAY "price / 4 [" PRICE-F "]". STOP RUN.

cobc -x -free -Wall -o muldiv muldiv.cob
./muldiv

12 * 5.25 = [000063.00]
total / 7 = [009.00]
100 / 7 = [014] rem [02]
price doubled [010.50]
price / 4 [002.62]

MULTIPLY QTY BY PRICE GIVING TOTAL is the form with three fields: the product of the first two goes into the third and nothing else changes. DIVIDE PARTS INTO TOTAL is the two-field form and divides the second by the first, so INTO names the divisor first. Without GIVING the receiver is also an operand, which is why MULTIPLY 2 BY PRICE doubles the price in place.

The digit the receiver cannot keep

The last line of the run is the lesson. Ten fifty divided by four is two and five eighths, and the field has two decimal places, so the result is 2.62: the digit is cut, not rounded, and the build says nothing because a division into a smaller picture is not an error.

A receiving picture that is too small for the whole part is worse, because the digits that are lost are the high ones:

IDENTIFICATION DIVISION. PROGRAM-ID. TINY2. DATA DIVISION. WORKING-STORAGE SECTION. 01 AMOUNT-F PIC 9(5)V99 VALUE 100.00. 01 SHARES-F PIC 9(2) VALUE 3. 01 TINY-F PIC 9(1)V99 VALUE 0. PROCEDURE DIVISION. DIVIDE SHARES-F INTO AMOUNT-F GIVING TINY-F. DISPLAY "tiny [" TINY-F "]". STOP RUN.

cobc -x -free -Wall -o tiny2 tiny2.cob
./tiny2

tiny [3.33]

One hundred divided by three is 33.33, and a field of one integer digit keeps 3.33. Nothing in the output says that the tens went missing; a field wide enough for the result is the only protection, and ON SIZE ERROR does not fire here, because a division that is cut to the receiving picture is not treated as an overflow.

REMAINDER

REMAINDER keeps what the integer part does not use, and with decimal operands it is the remainder after the quotient is scaled to the receiving picture:

IDENTIFICATION DIVISION. PROGRAM-ID. REM2. DATA DIVISION. WORKING-STORAGE SECTION. 01 TOTAL-F PIC 9(3)V99 VALUE 100.00. 01 PARTS-F PIC 9(2) VALUE 7. 01 EACH-F PIC 9(3)V99 VALUE 0. 01 REM-F PIC 9(3)V99 VALUE 0. PROCEDURE DIVISION. DIVIDE PARTS-F INTO TOTAL-F GIVING EACH-F REMAINDER REM-F. DISPLAY "each [" EACH-F "] rem [" REM-F "]". STOP RUN.

cobc -x -free -Wall -o rem2 rem2.cob
./rem2

each [014.28] rem [000.04]

A hundred split seven ways is fourteen and twenty-eight hundredths each, and four hundredths are left over. Seven times 14.28 is 99.96, so the check adds up: the quotient and the remainder together are the original amount. A split that has to be exact gives the cents to somebody, and the remainder is the value that says how many.

A division by zero is silent

The statement has no useful answer for a zero divisor, and without an error clause it produces one anyway:

IDENTIFICATION DIVISION. PROGRAM-ID. ZERO. DATA DIVISION. WORKING-STORAGE SECTION. 01 ZERO-F PIC 9(2) VALUE 0. 01 AMT-F PIC 9(5)V99 VALUE 100.00. 01 OUT-F PIC 9(5)V99 VALUE 0. PROCEDURE DIVISION. DIVIDE ZERO-F INTO AMT-F GIVING OUT-F. DISPLAY "out [" OUT-F "]". STOP RUN.

cobc -x -free -Wall -o zero zero.cob
./zero

out [00000.00]

The run ends with status zero and the field still holds what it held, so a program that divides by a count it has not checked reports the previous total and carries on. With ON SIZE ERROR the same statement takes its error branch instead, as the lesson on ON SIZE ERROR shows; a test of the divisor before the statement is the other half of the answer.

The receiver must be a field

GIVING takes a field, not a literal, and the compiler says so in the same words it uses for the other verbs:

IDENTIFICATION DIVISION. PROGRAM-ID. RULEREC. DATA DIVISION. WORKING-STORAGE SECTION. 01 A-VAL PIC 9(3) VALUE 12. 01 B-VAL PIC 9(3) VALUE 5. PROCEDURE DIVISION. MULTIPLY A-VAL BY B-VAL GIVING 5. DISPLAY A-VAL. STOP RUN.

cobc -x -free -Wall -o rulerec rulerec.cob

rulerec.cob:10: error: '5' is not a numeric or numeric-edited name
8 |
9 | PROCEDURE DIVISION.
10 > MULTIPLY A-VAL BY B-VAL GIVING 5.
11 | DISPLAY A-VAL.
12 | STOP RUN.

The other mistake is one of reading. DIVIDE 7 INTO 100 is 100 / 7, and DIVIDE 7 BY 100 is 7 / 100; the two statements are wrong for each other. With GIVING the division is always DIVIDE a INTO b GIVING c as c = b / a, and DIVIDE a BY b GIVING c as c = a / b, which is the order in which the words are read.

Exercises

  1. Change EACH-F to PIC 9(2)V99 and run the division of 63.00 by 7. What does the field hold, and what should it hold?
  2. Add ROUNDED to the division of the price by four and print the result. Which digit changed?
  3. Divide 100.00 by 7 with ROUNDED and REMAINDER together and print both. What is the relationship between them now?
  4. Guard the division by zero with an IF before the statement and print a message instead. Which of the two programs is easier to read afterwards?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does MULTIPLY A BY B do?

    Answer

    B becomes A times B; nothing else changes

    The field after BY is the receiver, and without GIVING it is also the second factor.

  2. What does DIVIDE A INTO B compute?

    Answer

    B divided by A, stored in B

    INTO names the divisor first and the dividend second, so the receiving field is the value being divided.

  3. What does REMAINDER give you?

    Answer

    What is left of the dividend after the quotient is stored

    The quotient and the remainder together account for the dividend, which is what makes a split auditable.

  4. What happens to a division whose result does not fit the receiving picture?

    Answer

    The high digits are cut silently unless the statement has ON SIZE ERROR

    A receiving picture that is too small truncates the value, and without the error clause nothing is reported.

  5. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q1. DATA DIVISION. WORKING-STORAGE SECTION. 01 QTY-F PIC 9(3) VALUE 12. 01 PRICE-F PIC 9(3)V99 VALUE 5.25. 01 TOTAL-F PIC 9(6)V99 VALUE 0. PROCEDURE DIVISION. MULTIPLY QTY-F BY PRICE-F GIVING TOTAL-F. DISPLAY "[" TOTAL-F "]". STOP RUN.

    Answer

    [000063.00]

    Twelve times five and a quarter is sixty-three, and the six-digit picture with two decimals prints it with the zeros in front.

  6. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q2. DATA DIVISION. WORKING-STORAGE SECTION. 01 AMOUNT-F PIC 9(5)V99 VALUE 100.00. 01 SHARES-F PIC 9(2) VALUE 3. 01 TINY-F PIC 9(1)V99 VALUE 0. PROCEDURE DIVISION. DIVIDE SHARES-F INTO AMOUNT-F GIVING TINY-F. DISPLAY "[" TINY-F "]". STOP RUN.

    Answer

    [3.33]

    One hundred divided by three is 33.33 and the field keeps one integer digit, so the tens are cut and the decimals follow the value.

  7. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q3. DATA DIVISION. WORKING-STORAGE SECTION. 01 TOTAL-F PIC 9(3)V99 VALUE 100.00. 01 PARTS-F PIC 9(2) VALUE 7. 01 EACH-F PIC 9(3)V99 VALUE 0. 01 REM-F PIC 9(3)V99 VALUE 0. PROCEDURE DIVISION. DIVIDE PARTS-F INTO TOTAL-F GIVING EACH-F REMAINDER REM-F. DISPLAY "[" EACH-F "][" REM-F "]". STOP RUN.

    Answer

    [014.28][000.04]

    Seven times 14.28 is 99.96, and the four hundredths that complete the hundred are the remainder.

  8. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q4. DATA DIVISION. WORKING-STORAGE SECTION. 01 ZERO-F PIC 9(2) VALUE 0. 01 AMT-F PIC 9(5)V99 VALUE 100.00. 01 OUT-F PIC 9(5)V99 VALUE 7.00. PROCEDURE DIVISION. DIVIDE ZERO-F INTO AMT-F GIVING OUT-F. DISPLAY "[" OUT-F "]". STOP RUN.

    Answer

    [00007.00]

    Without an error clause the statement leaves the receiving field as it was, so the seven that was in it is still there.

  9. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 PRICE-F PIC 9(3)V99 VALUE 5.25. PROCEDURE DIVISION. MULTIPLY 2 BY PRICE-F. DIVIDE 4 INTO PRICE-F. DISPLAY "[" PRICE-F "]". STOP RUN.

    Answer

    [002.62]

    The price doubles to 10.50 and the division by four cuts the third decimal of 2.625 instead of rounding it.

  10. What is the first line of the compiler output for this program? IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 A-VAL PIC 9(3) VALUE 12. 01 B-VAL PIC 9(3) VALUE 5. PROCEDURE DIVISION. MULTIPLY A-VAL BY B-VAL GIVING 5. DISPLAY A-VAL. STOP RUN.

    Answer

    error: '5' is not a numeric or numeric-edited name

    The field after GIVING must hold a number, and the compiler reports the literal with the wording it uses for a GIVING operand.

The code on this page was built with cobc (GnuCOBOL) 3.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev