Exercises — Chapter 5: Numeric Precision and Arithmetic

Exercise 5.1: Arithmetic Verb Forms

For each of the following operations, write the COBOL statement using: (a) the explicit arithmetic verb, and (b) the COMPUTE statement. Include ROUNDED and ON SIZE ERROR where appropriate.

  1. Add WS-DEPOSIT to ACCT-BALANCE
  2. Subtract WS-TAX from WS-GROSS giving WS-NET
  3. Multiply WS-HOURS by WS-RATE giving WS-PAY
  4. Divide WS-TOTAL by WS-COUNT giving WS-AVERAGE with remainder WS-LEFTOVER
  5. Calculate: RESULT = (A + B) * C / D

Exercise 5.2: Rounding Prediction

Given WS-SOURCE PIC S9(7)V9(4) COMP-3 and WS-TARGET PIC S9(7)V99 COMP-3, predict the result of COMPUTE WS-TARGET ROUNDED = WS-SOURCE for each source value:

  1. WS-SOURCE = +1234.5678
  2. WS-SOURCE = +1234.5650
  3. WS-SOURCE = +1234.5649
  4. WS-SOURCE = -1234.5650
  5. WS-SOURCE = +0.0050
  6. WS-SOURCE = +0.0049

Then predict the results again using ROUNDED MODE IS NEAREST-EVEN for values 2, 4, and 5.

Exercise 5.3: Intermediate Precision Analysis

Analyze the following COMPUTE statement. For each step, determine: (a) the intermediate result size, (b) whether precision could be lost, and (c) whether ARITH(EXTEND) is needed.

       77  WS-PRINCIPAL       PIC S9(13)V99 COMP-3.
       77  WS-RATE            PIC SV9(8)    COMP-3.
       77  WS-DAYS            PIC 9(3)      COMP.
       77  WS-YEAR-DAYS       PIC 9(3)      COMP VALUE 365.
       77  WS-INTEREST        PIC S9(11)V99 COMP-3.

           COMPUTE WS-INTEREST ROUNDED =
               WS-PRINCIPAL * WS-RATE * WS-DAYS / WS-YEAR-DAYS

Exercise 5.4: Floating-Point vs. Packed Decimal

Write a COBOL program that demonstrates the floating-point accumulation error. The program should:

  1. Declare a COMP-1 accumulator, a COMP-2 accumulator, and a COMP-3 accumulator
  2. Add 0.01 to each accumulator 10,000 times in a loop
  3. Display the final value of each accumulator
  4. Calculate and display the difference between each floating-point result and the COMP-3 result
  5. Repeat with adding 0.10 instead of 0.01 — is the error larger or smaller?

Exercise 5.5: Penny Distribution

Write a complete COBOL program that distributes a total amount across N recipients. Requirements:

  1. Accept a total amount (e.g., $1000.00) and number of recipients (e.g., 7)
  2. Use DIVIDE with REMAINDER to calculate the base share
  3. Distribute the remainder pennies to the first N recipients
  4. Display each recipient's share
  5. Verify that the sum of all shares equals the original total
  6. Test with these cases: $100.00 / 3, $1000.00 / 7, $1.00 / 3, $0.01 / 3

Exercise 5.6: Compound Interest Calculator

Write a COBOL program that:

  1. Accepts: principal, annual rate, compounding frequency (monthly/quarterly/daily), and number of years
  2. Calculates the final amount using the compound interest formula
  3. Displays a yearly summary showing the balance at the end of each year
  4. Uses COMP-3 with maximum intermediate precision
  5. Includes ON SIZE ERROR handling throughout
  6. Test with: $10,000 at 5% compounded monthly for 30 years

Exercise 5.7: Loan Amortization Schedule

Write a COBOL program that generates a loan amortization schedule:

  1. Accept: loan amount, annual interest rate, number of monthly payments
  2. Calculate the monthly payment using the standard formula
  3. For each payment, show: payment number, payment amount, interest portion, principal portion, remaining balance
  4. Handle the final payment adjustment so the balance reaches exactly zero
  5. Display totals: total payments, total interest, total principal
  6. Verify: total principal = original loan amount
  7. Test with: $200,000 at 6.5% for 360 months (30-year mortgage)

Exercise 5.8: Insurance Claim Proration

Write a COBOL program that prorates an insurance claim:

  1. Accept: total charge, allowed amount, deductible remaining, copay amount, coinsurance percentage
  2. Calculate: plan pays, member responsibility
  3. Verify: plan pays + member pays = allowed amount
  4. Handle edge cases: deductible exceeds allowed amount, zero copay, 100% coinsurance
  5. Test with these scenarios: - Charge $500, Allowed $400, Deductible $100, Copay $25, Coinsurance 20% - Charge $50, Allowed $50, Deductible $200, Copay $25, Coinsurance 20% - Charge $10000, Allowed $8000, Deductible $0, Copay $0, Coinsurance 0%

Exercise 5.9: SIZE ERROR Detective

The following program has a subtle arithmetic bug. Find it, explain what happens when specific values are used, and fix it.

       77  WS-PRICE           PIC S9(5)V99 COMP-3.
       77  WS-QTY             PIC 9(4) COMP.
       77  WS-DISCOUNT-PCT    PIC V99 COMP-3.
       77  WS-SUBTOTAL        PIC S9(7)V99 COMP-3.
       77  WS-DISCOUNT        PIC S9(5)V99 COMP-3.
       77  WS-TOTAL           PIC S9(7)V99 COMP-3.

           MOVE 999.99 TO WS-PRICE
           MOVE 500 TO WS-QTY
           MOVE .25 TO WS-DISCOUNT-PCT

           COMPUTE WS-SUBTOTAL = WS-PRICE * WS-QTY
           COMPUTE WS-DISCOUNT = WS-SUBTOTAL * WS-DISCOUNT-PCT
           SUBTRACT WS-DISCOUNT FROM WS-SUBTOTAL
               GIVING WS-TOTAL

Exercise 5.10: Performance Comparison

Design an experiment (as a COBOL program) to compare the performance of:

  1. COMP-3 arithmetic vs. COMP arithmetic for 1,000,000 addition operations
  2. COMPUTE with a complex expression vs. equivalent explicit verb sequences
  3. With and without ON SIZE ERROR

Use ACCEPT FROM TIME before and after each test to measure elapsed time. Document your findings. (Note: results will vary significantly between mainframe and GnuCOBOL environments.)