>>SOURCE FORMAT IS FREE *> cobol-compute-rounded.cob - COMPUTE with and without *> ROUNDED, ROUNDED MODE IS NEAREST-EVEN, an overflow trapped by *> ON SIZE ERROR, the same overflow untrapped, and a divide by zero *> Dialect: GnuCOBOL 3.x, free format *> Status : desk-checked against the GnuCOBOL 3.2 Programmer's Guide; *> compiled and run with GnuCOBOL 3.2 on 2026-10-29 *> Inputs : none *> Outputs: nine lines on standard output *> Build : cobc -x cobol-compute-rounded.cob *> Run : ./cobol-compute-rounded *> (Windows: cobol-compute-rounded.exe) IDENTIFICATION DIVISION. PROGRAM-ID. COMPUTE-DEMO. ENVIRONMENT DIVISION. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-Price PIC 9(3)V99 VALUE 19.99. 01 WS-Qty PIC 9(3) VALUE 3. 01 WS-Tax-Rate PIC V999 VALUE 0.075. 01 WS-Total-Trunc PIC 9(5)V99. 01 WS-Total-Round PIC 9(5)V99. 01 WS-In-A PIC 9V999 VALUE 2.345. 01 WS-In-B PIC 9V999 VALUE 2.355. 01 WS-Out-Default PIC 9V99. 01 WS-Out-Even PIC 9V99. 01 WS-Small PIC 9(3) VALUE 5. 01 WS-Big PIC 9(5) VALUE 12345. 01 WS-Divisor PIC 9 VALUE 0. 01 WS-Result PIC 9(5)V99 VALUE 0. PROCEDURE DIVISION. MAIN-PARAGRAPH. *> 1. The same expression, truncated and rounded. COMPUTE WS-Total-Trunc = WS-Price * WS-Qty * (1 + WS-Tax-Rate) COMPUTE WS-Total-Round ROUNDED = WS-Price * WS-Qty * (1 + WS-Tax-Rate) DISPLAY "Total, no ROUNDED : " WS-Total-Trunc DISPLAY "Total, ROUNDED : " WS-Total-Round *> 2. Rounding modes on a tie: 2.345 and 2.355 into two decimals. COMPUTE WS-Out-Default ROUNDED = WS-In-A COMPUTE WS-Out-Even ROUNDED MODE IS NEAREST-EVEN = WS-In-A DISPLAY "2.345 default mode : " WS-Out-Default DISPLAY "2.345 NEAREST-EVEN : " WS-Out-Even COMPUTE WS-Out-Default ROUNDED = WS-In-B COMPUTE WS-Out-Even ROUNDED MODE IS NEAREST-EVEN = WS-In-B DISPLAY "2.355 default mode : " WS-Out-Default DISPLAY "2.355 NEAREST-EVEN : " WS-Out-Even *> 3. Overflow trapped: the receiver keeps its old value. COMPUTE WS-Small = WS-Big ON SIZE ERROR DISPLAY "Size error trapped, WS-Small still " WS-Small NOT ON SIZE ERROR DISPLAY "No size error, WS-Small now " WS-Small END-COMPUTE *> 4. The same overflow untrapped: leading digits vanish silently. COMPUTE WS-Small = WS-Big DISPLAY "Untrapped overflow, WS-Small now " WS-Small *> 5. Division by zero, trapped instead of aborting the run. COMPUTE WS-Result = WS-Price / WS-Divisor ON SIZE ERROR DISPLAY "Divide by zero trapped, WS-Result still " WS-Result END-COMPUTE STOP RUN.