Suppose you were asked to write a simple COBOL program to calculate Joe Montana's pass completion percentage. You calculate it by dividing pass completions by pass attempts. (See 2.)
Suppose you were asked to write a simple COBOL program to calculate Joe Montana's pass completion percentage. You calculate it by dividing pass completions by pass attempts. (See Figure 2.)
A yellow flag is thrown for roughing the passer when you run this program using COBOL/400. The answer should be 64.6, not 60.0. The problem is that the IBM COBOL/400 compiler generates intermediate field sizes that are too small when used with the COMPUTE verb.
You'll encounter another problem when using the ROUNDED option. This option causes rounding at all intermediate stages of computation. Rounding should be done, in this case, once-at the end of the calculation.
3 shows the corrected program. It resolves the problem by creating a higher precision work field to receive the result, thereby causing the compiler to create more precise work fields. Then it rounds the result into the receiving field at the precision desired. Be aware that this method will not work for fields that approach the COBOL limit of 18 digits, which shouldn't be a problem unless you are trying to calculate Joe Montana's salary.
Figure 3 shows the corrected program. It resolves the problem by creating a higher precision work field to receive the result, thereby causing the compiler to create more precise work fields. Then it rounds the result into the receiving field at the precision desired. Be aware that this method will not work for fields that approach the COBOL limit of 18 digits, which shouldn't be a problem unless you are trying to calculate Joe Montana's salary.
COBOL Compute Thrown for a Loss
Figure 2 Original Completion Calculation Code
IDENTIFICATION DIVISION. PROGRAM-ID. CMP001CB. AUTHOR. TOM CONOVER DATE-WRITTEN. OCTOBER 27, 1994. * * JOE MONTANA'S STATS THRU WEEK EIGHT OF 1994-95 NFL SEASON * ENVIRONMENT DIVISION. CONFIGURATION SECTION. SOURCE-COMPUTER. IBM-AS400. OBJECT-COMPUTER. IBM-AS400. DATA DIVISION. WORKING-STORAGE SECTION. * 01 WS-PASS-COMPLETIONS PIC 9(3) VALUE 181. 01 WS-PASS-ATTEMPTS PIC 9(3) VALUE 280. 01 WS-COMPLETION-PERCENTAGE PIC 9(3)V9. 01 DETAIL-LINE. 05 DL-PASS-COMPLETIONS PIC 999. 05 FILLER PIC X(6) VALUE SPACES. 05 DL-PASS-ATTEMPTS PIC 999. 05 FILLER PIC X(9) VALUE SPACES. 05 DL-COMPLETION-PERCENTAGE PIC ZZ9.9. / PROCEDURE DIVISION. 000-MAINLINE. DISPLAY "COMP ATTEMPTS AVERAGE". * COMPUTE WS-COMPLETION-PERCENTAGE = (WS-PASS-COMPLETIONS / WS-PASS-ATTEMPTS) * 100 * MOVE WS-PASS-COMPLETIONS TO DL-PASS-COMPLETIONS. MOVE WS-PASS-ATTEMPTS TO DL-PASS-ATTEMPTS. MOVE WS-COMPLETION-PERCENTAGE TO DL-COMPLETION-PERCENTAGE. * DISPLAY DETAIL-LINE. GOBACK. *------------------------------------------------------------------------- * RESULTS: * >CALL CMP001CB * COMP ATTEMPTS AVERAGE * 181 280 60.0
COBOL Compute Thrown for a Loss
Figure 3 Working Completion Calculation Code
IDENTIFICATION DIVISION. PROGRAM-ID. CMP001CB. AUTHOR. TOM CONOVER DATE-WRITTEN. OCTOBER 27, 1994. * * JOE MONTANA'S STATS THRU WEEK EIGHT OF 1994-95 NFL SEASON * ENVIRONMENT DIVISION. CONFIGURATION SECTION. SOURCE-COMPUTER. IBM-AS400. OBJECT-COMPUTER. IBM-AS400. DATA DIVISION. WORKING-STORAGE SECTION. * 01 WS-PASS-COMPLETIONS PIC 9(3) VALUE 181. 01 WS-PASS-ATTEMPTS PIC 9(3) VALUE 280. 01 WS-COMPLETION-PERCENTAGE PIC 9(3)V9. 01 WS-HIGH-PRECISION-RESULT PIC 9(3)V9999. 01 DETAIL-LINE. 05 DL-PASS-COMPLETIONS PIC 999. 05 FILLER PIC X(6) VALUE SPACES. 05 DL-PASS-ATTEMPTS PIC 999. 05 FILLER PIC X(9) VALUE SPACES. 05 DL-COMPLETION-PERCENTAGE PIC ZZ9.9. / PROCEDURE DIVISION. 000-MAINLINE. DISPLAY "COMP ATTEMPTS AVERAGE". * COMPUTE WS-HIGH-PRECISION-RESULT = (WS-PASS-COMPLETIONS / WS-PASS-ATTEMPTS) * 100 * COMPUTE WS-COMPLETION-PERCENTAGE ROUNDED = WS-HIGH-PRECISION-RESULT * MOVE WS-PASS-COMPLETIONS TO DL-PASS-COMPLETIONS. MOVE WS-PASS-ATTEMPTS TO DL-PASS-ATTEMPTS. MOVE WS-COMPLETION-PERCENTAGE TO DL-COMPLETION-PERCENTAGE. * DISPLAY DETAIL-LINE. GOBACK. *------------------------------------------------------------------------- * RESULTS: * >CALL CMP002CB * COMP ATTEMPTS AVERAGE * 181 280 64.6
LATEST COMMENTS
MC Press Online