TExcelTAD
Home › COBOL › COBOL DIVIDE, ROUNDED & SIZE ERROR

COBOL DIVIDE, ROUNDED & SIZE ERROR

DIVIDE has the most forms of all the arithmetic verbs, and it is where rounding and overflow questions show up most often. This lesson covers DIVIDE, the ROUNDED phrase and the ON SIZE ERROR safety net, which work with all arithmetic verbs.

DIVIDE INTO and DIVIDE BY

Statement Means
DIVIDE a INTO b b = b / a
DIVIDE a INTO b GIVING c c = b / a
DIVIDE a BY b GIVING c c = a / b
DIVIDE a BY b GIVING c REMAINDER r c = a / b (truncated to c's picture), r = a - c * b

INTO and BY are opposites: "divide 4 into 20" and "divide 20 by 4" both mean 20 / 4. BY always needs GIVING.

cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. DIVIDE-DEMO.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-TOTAL       PIC 9(5)    VALUE 1000.
       01  WS-PEOPLE      PIC 99      VALUE 8.
       01  WS-SHARE       PIC 9(5)V99.
       01  WS-PIZZAS      PIC 999     VALUE 100.
       PROCEDURE DIVISION.
           DIVIDE WS-PEOPLE INTO WS-TOTAL GIVING WS-SHARE
           DISPLAY "1000 shared by 8: " WS-SHARE
           DIVIDE WS-TOTAL BY 3 GIVING WS-SHARE
           DISPLAY "1000 / 3:         " WS-SHARE
           DIVIDE 4 INTO WS-PIZZAS
           DISPLAY "100 / 4:          " WS-PIZZAS
           STOP RUN.
1000 shared by 8: 00125.00
1000 / 3:         00333.33
100 / 4:          025

REMAINDER

With an integer quotient, REMAINDER gives what is left over, which is handy for splitting items into boxes or minutes into hours:

cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. REMAINDER-DEMO.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-MINUTES     PIC 9(4)    VALUE 1385.
       01  WS-HOURS       PIC 99.
       01  WS-LEFT        PIC 99.
       01  WS-BOTTLES     PIC 999     VALUE 100.
       01  WS-CRATES      PIC 99.
       01  WS-LOOSE       PIC 99.
       PROCEDURE DIVISION.
           DIVIDE WS-MINUTES BY 60 GIVING WS-HOURS
               REMAINDER WS-LEFT
           DISPLAY WS-MINUTES " minutes = " WS-HOURS "h " WS-LEFT "m"
           DIVIDE WS-BOTTLES BY 12 GIVING WS-CRATES
               REMAINDER WS-LOOSE
           DISPLAY WS-CRATES " full crates, " WS-LOOSE " loose bottles"
           STOP RUN.
1385 minutes = 23h 05m
08 full crates, 04 loose bottles

ROUNDED

Without ROUNDED, extra decimals are truncated. With ROUNDED, the result is rounded half away from zero (.5 goes up in magnitude). This is what you want for money:

cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. ROUNDED-DEMO.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-BILL        PIC 9(4)V99 VALUE 100.00.
       01  WS-TRUNC       PIC 9(4)V99.
       01  WS-ROUND       PIC 9(4)V99.
       01  WS-WHOLE       PIC 9(4).
       PROCEDURE DIVISION.
           DIVIDE WS-BILL BY 3 GIVING WS-TRUNC
           DIVIDE WS-BILL BY 3 GIVING WS-ROUND ROUNDED
           DISPLAY "100 / 3 truncated: " WS-TRUNC
           DISPLAY "100 / 3 rounded:   " WS-ROUND
           DIVIDE 200 BY 3 GIVING WS-TRUNC
           DIVIDE 200 BY 3 GIVING WS-ROUND ROUNDED
           DISPLAY "200 / 3 truncated: " WS-TRUNC
           DISPLAY "200 / 3 rounded:   " WS-ROUND
           MULTIPLY 2.5 BY 1 GIVING WS-WHOLE ROUNDED
           DISPLAY "2.5 rounded to 9(4): " WS-WHOLE
           STOP RUN.
100 / 3 truncated: 0033.33
100 / 3 rounded:   0033.33
200 / 3 truncated: 0066.66
200 / 3 rounded:   0066.67
2.5 rounded to 9(4): 0003

Note: COBOL 2014 added ROUNDED MODE IS … with modes such as NEAREST-EVEN (banker's rounding) and TRUNCATION. Plain ROUNDED means NEAREST-AWAY-FROM-ZERO.

ON SIZE ERROR

A size error happens when a result has more integer digits than the receiver can hold, or on division by zero. Without a handler the result is silently truncated (or undefined for division by zero). With ON SIZE ERROR the receiver is left unchanged and your error code runs instead:

cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. SIZE-ERROR-DEMO.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-SMALL       PIC 99      VALUE 90.
       01  WS-RESULT      PIC 9(3)    VALUE 0.
       01  WS-DIVISOR     PIC 9       VALUE 0.
       PROCEDURE DIVISION.
           ADD 25 TO WS-SMALL
               ON SIZE ERROR
                   DISPLAY "Overflow! WS-SMALL kept " WS-SMALL
               NOT ON SIZE ERROR
                   DISPLAY "Sum stored: " WS-SMALL
           END-ADD
           ADD 5 TO WS-SMALL
               ON SIZE ERROR DISPLAY "Overflow again"
               NOT ON SIZE ERROR DISPLAY "Now WS-SMALL = " WS-SMALL
           END-ADD
           DIVIDE 100 BY WS-DIVISOR GIVING WS-RESULT
               ON SIZE ERROR DISPLAY "Cannot divide by zero"
           END-DIVIDE
           STOP RUN.
Overflow! WS-SMALL kept 90
Now WS-SMALL = 95
Cannot divide by zero

The scope terminators END-ADD, END-SUBTRACT, END-MULTIPLY, END-DIVIDE and END-COMPUTE mark where the ON SIZE ERROR statements end. Without them, the error branch would continue to the next period.

Test yourself

Which statement computes 20 / 4?

  1. DIVIDE 20 INTO 4 GIVING Q
  2. DIVIDE 4 INTO 20 GIVING Q
  3. DIVIDE 4 BY 20 GIVING Q
  4. DIVIDE Q BY 20 BY 4

What does ROUNDED do when 66.666… goes into PIC 9(4)V99?

  1. Stores 66.66
  2. Stores 66.67
  3. Stores 67.00
  4. Raises a size error

When ON SIZE ERROR fires, what value does the receiving item have?

  1. Zero
  2. The truncated result
  3. Its value from before the statement
  4. HIGH-VALUES