Loading...
Loading...
In COBOL, numeric data can be stored in several different internal formats, each identified by a USAGE clause. Understanding the difference between COMP, COMP-1, COMP-2, and COMP-3 is critical for data migration, mainframe integration, and legacy system interoperability.
You are building a data conversion utility that receives metadata about COBOL fields and must determine:
| Type | Also Known As | Storage | Decimal Support |
|---|---|---|---|
COMP | Binary | 2/4/8 bytes (based on digits) | No (implied) |
COMP-1 | Single-precision float | 4 bytes fixed | Yes |
COMP-2 | Double-precision float | 8 bytes fixed | Yes |
COMP-3 | Packed Decimal (BCD) | ⌈(digits+1)/2⌉ bytes | Yes |
COMP byte rules: 1–4 digits → 2 bytes, 5–9 digits → 4 bytes, 10–18 digits → 8 bytes
COMP-3 byte formula: ceil((digits + 1) / 2) bytes
Given a list of COBOL field descriptors, each containing a type and digits (number of significant digits in the PIC clause), return a list of results. Each result should be a string in the format:
"<TYPE>: <bytes> bytes, decimal=<true/false>"
"<TYPE>,<digits>" where:
TYPE is one of COMP, COMP-1, COMP-2, COMP-3digits is an integer (only relevant for COMP and COMP-3; for COMP-1/COMP-2 it will always be 0)A list of strings, one per field, formatted as:
"<TYPE>: <N> bytes, decimal=<true/false>"
Input: ["COMP,5", "COMP-3,7", "COMP-1,0"]
Output: ["COMP: 4 bytes, decimal=false", "COMP-3: 4 bytes, decimal=true", "COMP-1: 4 bytes, decimal=true"]
Explanation:
COMP with 5 digits → 5–9 digit range → 4 bytes, no native decimal1 <= fields.length <= 100
TYPE ∈ {"COMP", "COMP-1", "COMP-2", "COMP-3"}
0 <= digits <= 18
For COMP-1 and COMP-2, digits = 0
For COMP and COMP-3, 1 <= digits <= 18COMP-3 with 7 digits → ceil((7+1)/2) = 4 bytes, supports decimalCOMP-1 → always 4 bytes (single float), supports decimalInput: ["COMP-2,0", "COMP,3"]
Output: ["COMP-2: 8 bytes, decimal=true", "COMP: 2 bytes, decimal=false"]
Explanation:
COMP-2 → always 8 bytes (double float), supports decimalCOMP with 3 digits → 1–4 digit range → 2 bytes, no native decimalInput: ["COMP-3,1", "COMP,18", "COMP-3,9"]
Output: ["COMP-3: 1 bytes, decimal=true", "COMP: 8 bytes, decimal=false", "COMP-3: 5 bytes, decimal=true"]
Explanation:
COMP-3 with 1 digit → ceil(2/2) = 1 byteCOMP with 18 digits → 10–18 digit range → 8 bytesCOMP-3 with 9 digits → ceil(10/2) = 5 bytes(digits + 2) // 2 in integer arithmetic