Thursday, 4 September 2014

COBOL COMPUTE Statement: ROUNDED and ON SIZE ERROR

A COBOL billing program might need one result from several values: invoice amount, tax, discount, and service charge. The COMPUTE statement is the clean way to write that formula in one place instead of spreading the same arithmetic across separate ADD, SUBTRACT, MULTIPLY, and DIVIDE statements.

COBOL COMPUTE statement diagram showing input values, formula, result field, ROUNDED, and ON SIZE ERROR
Keep the formula in one place.

What is the COBOL COMPUTE statement?

The COMPUTE statement assigns the value of an arithmetic expression to one or more receiving data items. IBM notes that COMPUTE can combine arithmetic operations without the receiving-item restrictions that apply to separate arithmetic statements.

Use COMPUTE when the business rule is naturally a formula. Use a separate arithmetic verb when that verb is clearer for a simple operation or when your site standard prefers it.

Basic syntax

COMPUTE result-field = arithmetic-expression
    [ON SIZE ERROR imperative-statement]
    [NOT ON SIZE ERROR imperative-statement]
END-COMPUTE

END-COMPUTE is the explicit scope terminator. It is useful when the statement is nested inside IF, EVALUATE, or another conditional structure.

Simple COMPUTE example

This example calculates an invoice total from amount, tax, and discount fields. The receiving field needs enough digits and decimal places for the result.

01 WS-INVOICE-AMOUNT    PIC S9(7)V99 COMP-3.
01 WS-TAX-AMOUNT        PIC S9(5)V99 COMP-3.
01 WS-DISCOUNT-AMOUNT   PIC S9(5)V99 COMP-3.
01 WS-INVOICE-TOTAL     PIC S9(8)V99 COMP-3.

COMPUTE WS-INVOICE-TOTAL =
        WS-INVOICE-AMOUNT
      + WS-TAX-AMOUNT
      - WS-DISCOUNT-AMOUNT
END-COMPUTE.

Arithmetic operators

Operator Meaning Example
+ Addition A + B
- Subtraction A - B
* Multiplication QTY * PRICE
/ Division TOTAL / COUNT
** Exponentiation BASE ** 2

Operator order and parentheses

Multiplication, division, and exponentiation can change a result if the formula is written casually. Use parentheses when the business rule depends on a specific order.

COMPUTE WS-RESULT = WS-GROSS - WS-AMT * 1.3

* Same intent written more clearly:
COMPUTE WS-RESULT = WS-GROSS - (WS-AMT * 1.3)

* Different result:
COMPUTE WS-RESULT = (WS-GROSS - WS-AMT) * 1.3

In review, the second form is easier to defend because the intended order is visible.

ROUNDED phrase

Use ROUNDED when the calculated result can have more decimal places than the receiving field. Without ROUNDED, extra decimal places can be truncated when the result is stored.

01 WS-AVG-AMOUNT        PIC S9(7)V99 COMP-3.

COMPUTE WS-AVG-AMOUNT ROUNDED =
        WS-TOTAL-AMOUNT / WS-ITEM-COUNT
END-COMPUTE.

Rounding should match the report or finance rule. Do not add ROUNDED only because the result looks nicer on a test case.

ON SIZE ERROR

ON SIZE ERROR handles numeric overflow when the result cannot fit in the receiving field. IBM notes that COMPUTE with ON SIZE ERROR can detect significant left-order digit loss, while MOVE does not provide ON SIZE ERROR support.

COMPUTE WS-INVOICE-TOTAL =
        WS-INVOICE-AMOUNT + WS-TAX-AMOUNT
    ON SIZE ERROR
        MOVE 16 TO WS-RETURN-CODE
        DISPLAY 'INVOICE TOTAL TOO LARGE'
    NOT ON SIZE ERROR
        MOVE 0 TO WS-RETURN-CODE
END-COMPUTE.

Use this phrase for money, balances, counters, and any calculated field where silent truncation would create bad output.

COMPUTE vs MOVE

A single-operand COMPUTE can look like a MOVE, but the error-handling options differ. If the assignment can overflow and the program needs to react, COMPUTE ... ON SIZE ERROR is safer than a plain MOVE.

MOVE WS-SOURCE-AMOUNT TO WS-TARGET-AMOUNT

COMPUTE WS-TARGET-AMOUNT = WS-SOURCE-AMOUNT
    ON SIZE ERROR
        DISPLAY 'TARGET TOO SMALL'
END-COMPUTE

COMPUTE vs DIVIDE

The original title mentioned the DIVIDE verb. Keep the separate DIVIDE statement when quotient and remainder syntax makes the program easier to read. Use COMPUTE when division is only one part of a larger formula.

COMPUTE WS-UNIT-PRICE ROUNDED =
        WS-TOTAL-PRICE / WS-ITEM-COUNT
END-COMPUTE.

For full DIVIDE syntax and remainder examples, use the related DIVIDE guide linked below.

Common mistakes

Using a receiving field that is too small

If the result field cannot hold the answer, the calculation may truncate or raise a size error, depending on the phrases used. Size the receiving field for the largest valid business result.

Skipping END-COMPUTE in nested code

Periods can hide scope problems in old COBOL. Use END-COMPUTE when the statement is inside conditional logic.

Using COMPUTE for a formula nobody can read

A very long formula is still hard to maintain. Break it into named intermediate values when that makes the business rule easier to test.

Review checklist

  • Check every operand is numeric.
  • Use packed decimal for money unless the copybook says otherwise.
  • Use parentheses for business-rule order.
  • Use ROUNDED only when rounding is required.
  • Use ON SIZE ERROR when overflow must not be silent.
  • Compare test output against hand-calculated examples.

Video tutorial

This short tutorial walks through the same COBOL arithmetic idea in a visual format.

Related Mainframe Forum guides

For nearby topics, read COBOL DIVIDE statement, COBOL ROUNDED phrase, COBOL MULTIPLY statement, COBOL SUBTRACT statement, COBOL USAGE clause, and COBOL ARITH compiler option.

External references

IBM documents the COMPUTE statement, MOVE versus COMPUTE for arithmetic results, and COBOL arithmetic choices.

FAQ

What does COMPUTE do in COBOL?

COMPUTE evaluates an arithmetic expression and stores the result in one or more receiving data items.

When should I use ROUNDED with COMPUTE?

Use ROUNDED when the result can have more decimal places than the receiving field and the business rule says the value must be rounded.

What does ON SIZE ERROR do?

ON SIZE ERROR runs the specified statements when the result cannot fit in the receiving field.

Is COMPUTE better than DIVIDE?

Neither is always better. Use COMPUTE for formulas and use DIVIDE when quotient and remainder syntax makes the code clearer.

No comments:

Post a Comment

New In-feed ads