COMP-3, also known as packed decimal or packed BCD (Binary Coded Decimal), is one of COBOL's most efficient numeric data storage formats, particularly optimized for mainframe environments where storage space and processing efficiency are critical. This format stores two decimal digits per byte (except for the last byte which contains one digit and the sign), resulting in significant space savings compared to display numeric formats while maintaining the exact decimal precision required for financial and business calculations.
Understanding COMP-3 is essential for mainframe COBOL programming as it represents the preferred format for numeric data in high-volume transaction processing systems, financial applications, and any scenario where storage efficiency and decimal arithmetic precision are paramount. COMP-3 format enables faster arithmetic operations, reduces I/O overhead, and provides the exact decimal representation needed for monetary calculations without the precision issues that can occur with binary floating-point formats.
COMP-3 format uses Binary Coded Decimal (BCD) encoding where each decimal digit is represented using 4 bits (a nibble). Two digits are packed into each byte, with the rightmost nibble of the last byte reserved for the sign indicator. This packing results in approximately 50% space savings compared to display format while maintaining exact decimal representation.
The sign nibble uses specific hexadecimal values: C or F for positive numbers, and D for negative numbers. Some systems accept other sign values for positive numbers, but C and F are the most commonly generated values. This encoding allows COMP-3 numbers to maintain their sign information compactly within the data structure.
The storage requirement for a COMP-3 field is calculated as: (number of digits + 1) ÷ 2, rounded up to the nearest integer. For example, a PIC S9(7)V99 COMP-3 field (9 digits total) requires (9 + 1) ÷ 2 = 5 bytes of storage.
This compact storage format is particularly valuable in mainframe environments where millions of records may be processed, and the space savings can result in significant reductions in storage costs and I/O operations.
The "+1" in the calculation accounts for the sign nibble that occupies half of the last byte, even when the number of digits is even.
123456789101112131415161718192021222324252627282930313233343536373839DATA DIVISION. WORKING-STORAGE SECTION. *> Basic COMP-3 numeric fields 01 FINANCIAL-AMOUNTS. 05 ACCOUNT-BALANCE PIC S9(7)V99 COMP-3. *> 5 bytes 05 TRANSACTION-AMT PIC S9(5)V99 COMP-3. *> 4 bytes 05 INTEREST-RATE PIC S99V9999 COMP-3. *> 4 bytes 05 CUSTOMER-ID PIC 9(10) COMP-3. *> 6 bytes 05 RECORD-COUNT PIC S9(8) COMP-3. *> 5 bytes *> Different precision examples 01 PRECISION-EXAMPLES. 05 SMALL-AMOUNT PIC S9(3)V99 COMP-3. *> 3 bytes 05 LARGE-AMOUNT PIC S9(12)V99 COMP-3. *> 8 bytes 05 PERCENTAGE PIC S99V99 COMP-3. *> 3 bytes 05 WHOLE-NUMBER PIC S9(6) COMP-3. *> 4 bytes *> Value initialization 01 INITIALIZED-VALUES. 05 DEFAULT-BALANCE PIC S9(7)V99 COMP-3 VALUE ZERO. 05 MAX-LIMIT PIC S9(8)V99 COMP-3 VALUE 99999999.99. 05 MIN-PAYMENT PIC S9(4)V99 COMP-3 VALUE 25.00. PROCEDURE DIVISION. COMP3-BASIC-USAGE. *> Initialize COMP-3 fields MOVE 12345.67 TO ACCOUNT-BALANCE. MOVE 500.00 TO TRANSACTION-AMT. MOVE 5.25 TO INTEREST-RATE. *> Display values (automatic conversion to display format) DISPLAY "Account Balance: " ACCOUNT-BALANCE. DISPLAY "Transaction: " TRANSACTION-AMT. DISPLAY "Rate: " INTEREST-RATE "%". *> Arithmetic operations work normally ADD TRANSACTION-AMT TO ACCOUNT-BALANCE. COMPUTE ACCOUNT-BALANCE = ACCOUNT-BALANCE * (1 + INTEREST-RATE / 100).
PICTURE | Total Digits | Bytes Required | Calculation |
---|---|---|---|
S9(3) COMP-3 | 3 | 2 | (3+1)/2 = 2 |
S9(5)V99 COMP-3 | 7 | 4 | (7+1)/2 = 4 |
S9(7)V99 COMP-3 | 9 | 5 | (9+1)/2 = 5 |
S9(10) COMP-3 | 10 | 6 | (10+1)/2 = 5.5 → 6 |
S9(15)V99 COMP-3 | 17 | 9 | (17+1)/2 = 9 |
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758DATA DIVISION. WORKING-STORAGE SECTION. 01 FINANCIAL-CALCULATIONS. *> Account information - all COMP-3 for efficiency 05 PRINCIPAL-AMOUNT PIC S9(10)V99 COMP-3. 05 INTEREST-RATE PIC S99V9999 COMP-3. 05 COMPOUNDING-PERIODS PIC S999 COMP-3. 05 TIME-YEARS PIC S99 COMP-3. *> Calculation results 05 COMPOUND-FACTOR PIC S9(5)V9999 COMP-3. 05 FINAL-AMOUNT PIC S9(12)V99 COMP-3. 05 INTEREST-EARNED PIC S9(12)V99 COMP-3. *> Monthly payment calculation 05 MONTHLY-RATE PIC S9V999999 COMP-3. 05 TOTAL-PAYMENTS PIC S9(4) COMP-3. 05 PAYMENT-FACTOR PIC S9(8)V999999 COMP-3. 05 MONTHLY-PAYMENT PIC S9(6)V99 COMP-3. 01 DISPLAY-FIELDS. 05 DISPLAY-PRINCIPAL PIC Z,ZZZ,ZZ9.99. 05 DISPLAY-RATE PIC Z9.9999. 05 DISPLAY-FINAL PIC Z,ZZZ,ZZZ,ZZ9.99. 05 DISPLAY-PAYMENT PIC Z,ZZ9.99. PROCEDURE DIVISION. FINANCIAL-DEMO. *> Initialize loan parameters MOVE 150000.00 TO PRINCIPAL-AMOUNT. MOVE 6.5000 TO INTEREST-RATE. MOVE 30 TO TIME-YEARS. MOVE 12 TO COMPOUNDING-PERIODS. *> Calculate compound interest COMPUTE MONTHLY-RATE = INTEREST-RATE / 100 / COMPOUNDING-PERIODS. COMPUTE TOTAL-PAYMENTS = TIME-YEARS * COMPOUNDING-PERIODS. COMPUTE COMPOUND-FACTOR = (1 + MONTHLY-RATE) ** TOTAL-PAYMENTS. COMPUTE FINAL-AMOUNT = PRINCIPAL-AMOUNT * COMPOUND-FACTOR. COMPUTE INTEREST-EARNED = FINAL-AMOUNT - PRINCIPAL-AMOUNT. *> Calculate monthly payment COMPUTE PAYMENT-FACTOR = MONTHLY-RATE / (1 - (1 + MONTHLY-RATE) ** (-TOTAL-PAYMENTS)). COMPUTE MONTHLY-PAYMENT = PRINCIPAL-AMOUNT * PAYMENT-FACTOR. *> Display results with formatting MOVE PRINCIPAL-AMOUNT TO DISPLAY-PRINCIPAL. MOVE INTEREST-RATE TO DISPLAY-RATE. MOVE FINAL-AMOUNT TO DISPLAY-FINAL. MOVE MONTHLY-PAYMENT TO DISPLAY-PAYMENT. DISPLAY "Loan Principal: $" DISPLAY-PRINCIPAL. DISPLAY "Interest Rate: " DISPLAY-RATE "% annually". DISPLAY "Term: " TIME-YEARS " years". DISPLAY "Final Amount: $" DISPLAY-FINAL. DISPLAY "Monthly Payment: $" DISPLAY-PAYMENT.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879DATA DIVISION. WORKING-STORAGE SECTION. 01 BATCH-PROCESSING-COUNTERS. 05 RECORDS-READ PIC S9(8) COMP-3 VALUE ZERO. 05 RECORDS-PROCESSED PIC S9(8) COMP-3 VALUE ZERO. 05 RECORDS-ERROR PIC S9(6) COMP-3 VALUE ZERO. 05 TOTAL-AMOUNT PIC S9(12)V99 COMP-3 VALUE ZERO. 05 BATCH-TOTAL PIC S9(10)V99 COMP-3 VALUE ZERO. 01 RECORD-STRUCTURE. 05 TRANSACTION-RECORD. 10 CUSTOMER-ID PIC 9(10) COMP-3. 10 TRANSACTION-TYPE PIC X. 10 TRANSACTION-AMT PIC S9(7)V99 COMP-3. 10 TRANSACTION-DATE PIC 9(8) COMP-3. 10 BRANCH-CODE PIC 9(4) COMP-3. 01 VALIDATION-LIMITS. 05 MIN-TRANSACTION PIC S9(5)V99 COMP-3 VALUE 0.01. 05 MAX-TRANSACTION PIC S9(7)V99 COMP-3 VALUE 50000.00. 05 MAX-BATCH-SIZE PIC S9(6) COMP-3 VALUE 10000. PROCEDURE DIVISION. HIGH-VOLUME-PROCESSING. PERFORM UNTIL END-OF-FILE = 'Y' READ TRANSACTION-FILE INTO TRANSACTION-RECORD AT END MOVE 'Y' TO END-OF-FILE NOT AT END ADD 1 TO RECORDS-READ PERFORM VALIDATE-AND-PROCESS-RECORD END-READ *> Check batch limits IF RECORDS-PROCESSED >= MAX-BATCH-SIZE PERFORM PROCESS-BATCH-TOTALS MOVE ZERO TO RECORDS-PROCESSED MOVE ZERO TO BATCH-TOTAL END-IF END-PERFORM. *> Final batch processing IF RECORDS-PROCESSED > ZERO PERFORM PROCESS-BATCH-TOTALS END-IF. VALIDATE-AND-PROCESS-RECORD. *> Validate transaction amount IF TRANSACTION-AMT >= MIN-TRANSACTION AND TRANSACTION-AMT <= MAX-TRANSACTION *> Process valid transaction ADD TRANSACTION-AMT TO TOTAL-AMOUNT ADD TRANSACTION-AMT TO BATCH-TOTAL ADD 1 TO RECORDS-PROCESSED *> Apply business logic based on transaction type EVALUATE TRANSACTION-TYPE WHEN 'D' *> Deposit PERFORM PROCESS-DEPOSIT WHEN 'W' *> Withdrawal PERFORM PROCESS-WITHDRAWAL WHEN 'T' *> Transfer PERFORM PROCESS-TRANSFER WHEN OTHER ADD 1 TO RECORDS-ERROR DISPLAY "Invalid transaction type: " TRANSACTION-TYPE END-EVALUATE ELSE ADD 1 TO RECORDS-ERROR DISPLAY "Invalid amount: " TRANSACTION-AMT END-IF. PROCESS-BATCH-TOTALS. DISPLAY "Batch Summary:". DISPLAY "Records Processed: " RECORDS-PROCESSED. DISPLAY "Batch Total: $" BATCH-TOTAL. DISPLAY "Running Total: $" TOTAL-AMOUNT. DISPLAY "Error Records: " RECORDS-ERROR.
Format | Storage (9 digits) | Arithmetic Speed | Best Use Case |
---|---|---|---|
PIC 9(9) | 9 bytes | Slow | Display/Reports |
PIC 9(9) COMP | 4 bytes | Fast | Counters/Indexes |
PIC 9(9) COMP-3 | 5 bytes | Very Fast | Financial/Decimal |
PIC 9(9) COMP-1 | 4 bytes | Very Fast | Scientific/Float |
12345678910111213141516171819202122232425262728293031DATA DIVISION. WORKING-STORAGE SECTION. *> Comparison of different numeric formats 01 FORMAT-COMPARISON. 05 DISPLAY-NUMBER PIC S9(7)V99. *> 10 bytes 05 BINARY-NUMBER PIC S9(7)V99 COMP. *> 8 bytes 05 PACKED-NUMBER PIC S9(7)V99 COMP-3. *> 5 bytes 05 FLOAT-NUMBER PIC S9(7)V99 COMP-1. *> 4 bytes 01 TEST-VALUE PIC S9(7)V99 VALUE 1234567.89. PROCEDURE DIVISION. FORMAT-COMPARISON-TEST. *> Move same value to different formats MOVE TEST-VALUE TO DISPLAY-NUMBER. MOVE TEST-VALUE TO BINARY-NUMBER. MOVE TEST-VALUE TO PACKED-NUMBER. MOVE TEST-VALUE TO FLOAT-NUMBER. *> All should display the same value DISPLAY "Display format: " DISPLAY-NUMBER. DISPLAY "Binary format: " BINARY-NUMBER. DISPLAY "Packed format: " PACKED-NUMBER. DISPLAY "Float format: " FLOAT-NUMBER. *> But storage efficiency varies significantly DISPLAY "Storage requirements:". DISPLAY "Display: 10 bytes". DISPLAY "Binary: 8 bytes". DISPLAY "Packed: 5 bytes (BEST for decimal)". DISPLAY "Float: 4 bytes (precision may vary)".
COMP-3 fields can become corrupted if invalid data is moved into them. Always validate numeric data before moving it to COMP-3 fields, especially when reading from external sources or user input.
Use the NUMERIC test to verify that data is valid before arithmetic operations. Invalid COMP-3 data can cause program abends or incorrect calculations.
When interfacing with non-COBOL systems, ensure proper conversion between COMP-3 and other numeric formats to prevent data corruption or misinterpretation.
1234567891011121314151617181920212223242526272829303132333435363738DATA DIVISION. WORKING-STORAGE SECTION. 01 VALIDATION-EXAMPLE. 05 INPUT-FIELD PIC X(10). 05 NUMERIC-FIELD PIC S9(7)V99 COMP-3. 05 VALIDATION-FLAGS. 10 IS-NUMERIC PIC X VALUE 'N'. 10 IS-VALID-RANGE PIC X VALUE 'N'. PROCEDURE DIVISION. SAFE-COMP3-ASSIGNMENT. ACCEPT INPUT-FIELD. *> First check if input is numeric IF INPUT-FIELD IS NUMERIC MOVE 'Y' TO IS-NUMERIC *> Check if within acceptable range IF INPUT-FIELD >= ZERO AND INPUT-FIELD <= 99999.99 MOVE 'Y' TO IS-VALID-RANGE MOVE INPUT-FIELD TO NUMERIC-FIELD DISPLAY "Valid COMP-3 assignment: " NUMERIC-FIELD ELSE DISPLAY "Error: Value out of range" END-IF ELSE DISPLAY "Error: Non-numeric input" END-IF. COMP3-INTEGRITY-CHECK. *> Verify COMP-3 field integrity IF NUMERIC-FIELD IS NUMERIC DISPLAY "COMP-3 field is valid" PERFORM CONTINUE-PROCESSING ELSE DISPLAY "Error: COMP-3 field corrupted" PERFORM ERROR-RECOVERY END-IF.
Use COMP-3 for financial calculations, currency amounts, and any decimal arithmetic where exact precision is required. Use COMP for integer counters and indexes. Use COMP-1/COMP-2 for scientific calculations that can tolerate floating-point precision issues.
Yes, COMP-3 fields fully support decimal places using the V in the PICTURE clause. The decimal point is implied and doesn't take additional storage space.
COBOL automatically handles conversion when you MOVE between COMP-3 and display numeric fields. The conversion is transparent as long as the receiving field is large enough to hold the value.
Invalid data in COMP-3 fields can cause data exceptions, incorrect calculations, or program abends. Always validate numeric data before moving it to COMP-3 fields and use the NUMERIC test to verify field integrity.
Design a customer record structure using COMP-3 fields for all numeric data. Calculate the storage savings compared to using display format for the same data.
Create a mortgage calculator using COMP-3 fields for all monetary amounts and calculations. Include validation to ensure data integrity throughout the calculation process.
Develop a high-volume transaction processing system using COMP-3 for all counters, amounts, and numeric fields. Include comprehensive error handling and data validation.
Complete overview of all COBOL numeric and character data types.
Understanding COMP, COMP-1, COMP-2, and other USAGE formats.
Defining numeric field sizes and decimal precision.
Performing calculations with COMP-3 and other numeric formats.