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.
- Add WS-DEPOSIT to ACCT-BALANCE
- Subtract WS-TAX from WS-GROSS giving WS-NET
- Multiply WS-HOURS by WS-RATE giving WS-PAY
- Divide WS-TOTAL by WS-COUNT giving WS-AVERAGE with remainder WS-LEFTOVER
- 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:
- WS-SOURCE = +1234.5678
- WS-SOURCE = +1234.5650
- WS-SOURCE = +1234.5649
- WS-SOURCE = -1234.5650
- WS-SOURCE = +0.0050
- 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:
- Declare a COMP-1 accumulator, a COMP-2 accumulator, and a COMP-3 accumulator
- Add 0.01 to each accumulator 10,000 times in a loop
- Display the final value of each accumulator
- Calculate and display the difference between each floating-point result and the COMP-3 result
- 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:
- Accept a total amount (e.g., $1000.00) and number of recipients (e.g., 7)
- Use DIVIDE with REMAINDER to calculate the base share
- Distribute the remainder pennies to the first N recipients
- Display each recipient's share
- Verify that the sum of all shares equals the original total
- 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:
- Accepts: principal, annual rate, compounding frequency (monthly/quarterly/daily), and number of years
- Calculates the final amount using the compound interest formula
- Displays a yearly summary showing the balance at the end of each year
- Uses COMP-3 with maximum intermediate precision
- Includes ON SIZE ERROR handling throughout
- 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:
- Accept: loan amount, annual interest rate, number of monthly payments
- Calculate the monthly payment using the standard formula
- For each payment, show: payment number, payment amount, interest portion, principal portion, remaining balance
- Handle the final payment adjustment so the balance reaches exactly zero
- Display totals: total payments, total interest, total principal
- Verify: total principal = original loan amount
- 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:
- Accept: total charge, allowed amount, deductible remaining, copay amount, coinsurance percentage
- Calculate: plan pays, member responsibility
- Verify: plan pays + member pays = allowed amount
- Handle edge cases: deductible exceeds allowed amount, zero copay, 100% coinsurance
- 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:
- COMP-3 arithmetic vs. COMP arithmetic for 1,000,000 addition operations
- COMPUTE with a complex expression vs. equivalent explicit verb sequences
- 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.)