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 syntax at a glance
| Form | Operation | Receiver |
|---|---|---|
DIVIDE A INTO B | B ÷ A | B |
DIVIDE A INTO B GIVING C | B ÷ A | C |
DIVIDE A BY B GIVING C | A ÷ B | C |
DIVIDE A INTO B GIVING C REMAINDER D | B ÷ A | C and D |
DIVIDE A BY B GIVING C REMAINDER D | A ÷ B | C and D |
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
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
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
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
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
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.
Handle divide-by-zero and overflow with ON SIZE ERROR
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
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 Bmeans 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
ROUNDEDapplies 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
- DIVIDE statement formats and rules
- ROUNDED phrase
- SIZE ERROR phrases
- Handling arithmetic errors
- Intermediate results and arithmetic precision
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.
ON SIZE ERROR, and use END-DIVIDE when the statement contains conditional phrases.
No comments:
Post a Comment