Thursday, 4 September 2014

COBOL DIVIDE Statement: INTO, BY, GIVING, and REMAINDER

DIVIDE 125 BY 12 GIVING WS-QUOTIENT REMAINDER WS-REMAINDER stores 10 as the integer quotient and 5 as the remainder. The direction words matter: INTO and BY reverse the operand order.

COBOL DIVIDE statement showing dividend divisor quotient and remainder
DIVIDE can replace an operand or place the quotient and remainder in separate fields.

COBOL DIVIDE statement syntax at a glance

FormOperationReceiver
DIVIDE A INTO BB ÷ AB
DIVIDE A INTO B GIVING CB ÷ AC
DIVIDE A BY B GIVING CA ÷ BC
DIVIDE A INTO B GIVING C REMAINDER DB ÷ AC and D
DIVIDE A BY B GIVING C REMAINDER DA ÷ BC and D
Memory aid: read DIVIDE 4 INTO 20 as “put 4 into 20,” which yields 5. Read DIVIDE 20 BY 4 in normal mathematical order, which also yields 5.

Working-Storage fields for the examples

01 WS-DIVIDEND PIC 9(5)V99 VALUE 125.00. 01 WS-DIVISOR PIC 9(3)V99 VALUE 12.00. 01 WS-QUOTIENT PIC 9(5)V99 VALUE ZERO. 01 WS-WHOLE-QUOTIENT PIC 9(5) VALUE ZERO. 01 WS-REMAINDER PIC 9(3)V99 VALUE ZERO. 01 WS-ERROR-SW PIC X VALUE 'N'.

The receiver's PICTURE controls how many integer and fractional positions can be stored. Review the COBOL Data Division guide when selecting DISPLAY, COMP, or COMP-3 fields.

Format 1: DIVIDE INTO replaces the operand

MOVE 100 TO WS-DIVIDEND DIVIDE 4 INTO WS-DIVIDEND * WS-DIVIDEND now contains 25

The divisor is 4 and the original receiver is 100. Format 1 changes the receiver itself. It can name multiple receivers, processed from left to right, but separate statements are often easier to diagnose.

Formats 2 and 3: preserve both operands with GIVING

DIVIDE 4 INTO 100 GIVING WS-QUOTIENT DIVIDE 100 BY 4 GIVING WS-QUOTIENT

Both statements produce 25 in WS-QUOTIENT. Neither input is changed. Use this form when later logic still needs the dividend and divisor. For expressions containing several operations, compare it with the COBOL COMPUTE statement.

Formats 4 and 5: quotient and REMAINDER

DIVIDE 125 BY 12 GIVING WS-WHOLE-QUOTIENT REMAINDER WS-REMAINDER ON SIZE ERROR MOVE 'Y' TO WS-ERROR-SW END-DIVIDE

The quotient is 10 and the remainder is 5. IBM defines the remainder as the dividend minus the product of the divisor and the intermediate quotient. For the REMAINDER formats, that intermediate quotient is truncated rather than rounded. Floating-point operands and receivers are not allowed in formats 4 and 5.

ROUNDED versus truncation

DIVIDE 10 BY 6 GIVING WS-QUOTIENT DIVIDE 10 BY 6 GIVING WS-QUOTIENT ROUNDED

If WS-QUOTIENT has two decimal positions, the unrounded result stores 1.66 and the rounded result stores 1.67. COBOL normally truncates excess fractional positions unless ROUNDED is specified. See the focused COBOL ROUNDED clause guide for receiver-scale examples.

Boundary: the DIVIDE formats that return a REMAINDER do not use the ROUNDED phrase. Choose a fractional quotient or an integer quotient plus remainder according to the business rule.

Handle divide-by-zero and overflow with ON SIZE ERROR

IF WS-DIVISOR = ZERO MOVE 'Y' TO WS-ERROR-SW ELSE DIVIDE WS-DIVIDEND BY WS-DIVISOR GIVING WS-QUOTIENT ROUNDED ON SIZE ERROR MOVE 'Y' TO WS-ERROR-SW NOT ON SIZE ERROR MOVE 'N' TO WS-ERROR-SW END-DIVIDE END-IF

Division by zero and a final result too large for its receiver are size errors. When a size-error phrase is present, COBOL runs the ON SIZE ERROR path and the affected result field is not changed. The explicit zero check documents the expected input rule; ON SIZE ERROR still protects the arithmetic statement.

For fixed-point and decimal overflow handling, IBM also documents a dependency on the Language Environment TRAP(ON) runtime option. Review site runtime settings when expected size errors are not taking the controlled path.

Practical unit-price example

IF WS-ITEM-COUNT > ZERO DIVIDE WS-ORDER-TOTAL BY WS-ITEM-COUNT GIVING WS-UNIT-PRICE ROUNDED ON SIZE ERROR MOVE 'Y' TO WS-PRICE-ERROR END-DIVIDE ELSE MOVE 'Y' TO WS-PRICE-ERROR END-IF

Define WS-UNIT-PRICE with the currency scale required by the application. Do not treat binary storage as permission to exceed the PICTURE boundary; the COBOL TRUNC compiler option has its own rules.

Common DIVIDE mistakes

  • Reversing INTO: DIVIDE A INTO B means B divided by A.
  • Using an undersized quotient: receiver overflow raises a size error when the clause is coded.
  • Expecting automatic decimal rounding: excess fractional digits are truncated unless ROUNDED applies and is specified.
  • Combining REMAINDER with floating point: formats 4 and 5 do not accept floating-point operands or receivers.
  • Omitting END-DIVIDE in nested logic: the explicit scope terminator prevents ambiguous conditional scope.

Related arithmetic verbs have separate intent pages: COBOL MULTIPLY and COBOL SUBTRACT.

Official IBM references

Frequently asked questions

What is the difference between DIVIDE INTO and DIVIDE BY in COBOL?

DIVIDE A INTO B calculates B divided by A. DIVIDE A BY B calculates A divided by B. With GIVING, the quotient is stored in a separate receiver.

How does COBOL handle division by zero?

Division by zero is a size error. Code ON SIZE ERROR to take a controlled path and leave the affected result field unchanged instead of relying on environment-dependent failure handling.

Can COBOL DIVIDE return a quotient and remainder?

Yes. Use GIVING for the quotient and REMAINDER for the remainder. REMAINDER is available with the INTO and BY forms that include GIVING.

Does ROUNDED affect the COBOL REMAINDER value?

For DIVIDE formats with REMAINDER, COBOL calculates the remainder from a truncated intermediate quotient. ROUNDED is not part of those formats.

Production rule: define the quotient's scale deliberately, check a zero divisor, code ON SIZE ERROR, and use END-DIVIDE when the statement contains conditional phrases.

No comments:

Post a Comment