COBOL COMPUTE
The arithmetic verbs are readable for one operation, but a formula such as
total = price × quantity × (1 + tax rate) − discount would take four statements. COMPUTE evaluates a whole
arithmetic expression and stores the result:
COMPUTE receiver [ROUNDED] = arithmetic-expression
[ON SIZE ERROR ...] [NOT ON SIZE ERROR ...]
[END-COMPUTE]Operators
| Operator | Meaning | Precedence |
|---|---|---|
( ) |
Grouping | highest |
+ - (unary) |
Sign | |
** |
Exponentiation | |
* / |
Multiply, divide | |
+ - |
Add, subtract | lowest |
Operators must be surrounded by spaces: A - B subtracts, while A-B is read as one name with a hyphen.
That is the most common COMPUTE mistake.
IDENTIFICATION DIVISION.
PROGRAM-ID. COMPUTE-DEMO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-PRICE PIC 9(4)V99 VALUE 24.99.
01 WS-QTY PIC 9(3) VALUE 4.
01 WS-TAX-RATE PIC V999 VALUE .075.
01 WS-DISCOUNT PIC 9(3)V99 VALUE 5.00.
01 WS-TOTAL PIC 9(5)V99.
PROCEDURE DIVISION.
COMPUTE WS-TOTAL ROUNDED =
WS-PRICE * WS-QTY * (1 + WS-TAX-RATE) - WS-DISCOUNT
DISPLAY "Total: " WS-TOTAL
STOP RUN.Total: 00102.46
24.99 × 4 = 99.96, plus 7.5% tax is 107.457, minus 5.00 is 102.457, which rounds to 102.46.
Precedence and parentheses
IDENTIFICATION DIVISION.
PROGRAM-ID. PRECEDENCE.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-R1 PIC S9(4).
01 WS-R2 PIC S9(4).
01 WS-R3 PIC S9(4).
01 WS-R4 PIC S9(4).
PROCEDURE DIVISION.
COMPUTE WS-R1 = 2 + 3 * 4
COMPUTE WS-R2 = (2 + 3) * 4
COMPUTE WS-R3 = 2 ** 3 ** 2
COMPUTE WS-R4 = -2 ** 2
DISPLAY "2 + 3 * 4 = " WS-R1
DISPLAY "(2 + 3) * 4 = " WS-R2
DISPLAY "2 ** 3 ** 2 = " WS-R3
DISPLAY "-2 ** 2 = " WS-R4
STOP RUN.2 + 3 * 4 = +0014
(2 + 3) * 4 = +0020
2 ** 3 ** 2 = +0512
-2 ** 2 = +0004
Two results deserve a closer look. GnuCOBOL evaluates a chain of ** from right to left, so 2 ** 3 ** 2 is
2 ** 9 = 512, and the unary minus in -2 ** 2 applies first, giving (-2) ** 2 = 4. Compilers have not always
agreed on these cases, so always write parentheses around exponentiation chains and negative bases.
Compound interest with **
Exponentiation makes financial formulas short. Here is the value of a deposit after n years of annual
compounding, A = P × (1 + r)ⁿ:
IDENTIFICATION DIVISION.
PROGRAM-ID. INTEREST.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-PRINCIPAL PIC 9(7)V99 VALUE 10000.00.
01 WS-RATE PIC V9(4) VALUE .0350.
01 WS-YEARS PIC 99 VALUE 10.
01 WS-AMOUNT PIC 9(7)V99.
01 WS-EARNED PIC 9(7)V99.
PROCEDURE DIVISION.
COMPUTE WS-AMOUNT ROUNDED =
WS-PRINCIPAL * (1 + WS-RATE) ** WS-YEARS
COMPUTE WS-EARNED = WS-AMOUNT - WS-PRINCIPAL
DISPLAY "After " WS-YEARS " years: " WS-AMOUNT
DISPLAY "Interest earned: " WS-EARNED
STOP RUN.After 10 years: 0014105.99
Interest earned: 0004105.99
Intermediate precision
COBOL truncates or rounds when it stores into the receiver, but the compiler also has to choose how many decimals to keep for each intermediate result. A division in the middle of an expression can therefore lose digits, so prefer multiplying before dividing:
IDENTIFICATION DIVISION.
PROGRAM-ID. ORDER-OF-OPS.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-A PIC 9(3)V99.
01 WS-B PIC 9(3)V99.
01 WS-STEP PIC 9(3)V99.
PROCEDURE DIVISION.
COMPUTE WS-A = 10 / 3 * 3
COMPUTE WS-B = 10 * 3 / 3
COMPUTE WS-STEP = 10 / 3
COMPUTE WS-STEP = WS-STEP * 3
DISPLAY "10 / 3 * 3 in one COMPUTE: " WS-A
DISPLAY "10 * 3 / 3: " WS-B
DISPLAY "Two steps via 9(3)V99: " WS-STEP
STOP RUN.10 / 3 * 3 in one COMPUTE: 009.99
10 * 3 / 3: 010.00
Two steps via 9(3)V99: 009.99
10 / 3 * 3 gives 9.99 here: the quotient 3.333… was cut to the receiver's two decimals before the
multiplication, exactly like the two-step version that stores 3.33 in between. Writing 10 * 3 / 3 divides last
and gives the exact 10.00. That is a real-world rounding drift: multiply first, divide last, and give
intermediate fields enough decimals.
Size errors in COMPUTE
ON SIZE ERROR works exactly as with the other verbs:
IDENTIFICATION DIVISION.
PROGRAM-ID. COMPUTE-SIZE.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-SQUARE PIC 9(3).
01 WS-N PIC 9(3) VALUE 40.
PROCEDURE DIVISION.
COMPUTE WS-SQUARE = WS-N ** 2
ON SIZE ERROR DISPLAY "40 squared does not fit in 9(3)"
NOT ON SIZE ERROR DISPLAY "Square: " WS-SQUARE
END-COMPUTE
MOVE 30 TO WS-N
COMPUTE WS-SQUARE = WS-N ** 2
ON SIZE ERROR DISPLAY "30 squared does not fit"
NOT ON SIZE ERROR DISPLAY "30 squared: " WS-SQUARE
END-COMPUTE
STOP RUN.40 squared does not fit in 9(3)
30 squared: 900
Test yourself
What is wrong with COMPUTE WS-NET = WS-GROSS-WS-TAX?
- Nothing
- COMPUTE cannot subtract
- Without spaces,
WS-GROSS-WS-TAXis read as one data name - The result needs ROUNDED
What does COMPUTE R = 2 + 3 * 4 store?
- 20
- 14
- 24
- 9
Which operator is exponentiation in COBOL?
- ^
- **
- POW
- EXP