COBOL Compute Thrown for a Loss

General
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

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 
BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  •  

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: