In IBM COBOL, the COMP (computational) data type is used to store numeric values in binary format, ideal for efficient arithmetic operations. This format requires the data to be integers, and the compiler allocates memory in word-sized chunks:
- Half word = 2 bytes
- Full word = 4 bytes
- Double word = 8 bytes
Storage Allocation for COMP Variables
When defining a variable like WS-NUM PIC S9(n) USAGE IS COMP, the storage size depends on the number of digits (n):
- n = 1 to 4 → 2 bytes
- n = 5 to 9 → 4 bytes
- n = 10 to 18 → 8 bytes
For example, PIC S9(4) COMP can store values from -9999 to +9999, limited by the number of digits specified in the PIC clause.
What Is COMP-5 and Why Use It?
To overcome the limitations of COMP, IBM introduced COMP-5, which allows binary fields to behave like native binary integers. This means the value range is determined by the binary field size, not the number of digits in the picture clause.
For instance:
PIC S9(4) COMP-5can store values from -32768 to +32767PIC 9 COMP-5andPIC 999 COMP-5can store values from 0 to 65535
COMP vs COMP-5: Key Differences
| Picture Clause | COMP Range | COMP-5 Range |
|---|---|---|
PIC 9 | 0 to 9 | 0 to 65535 |
PIC S99 | -99 to +99 | -32768 to +32767 |
PIC 999 | 0 to 999 | 0 to 65535 |
COMP truncates based on the decimal value defined in the picture clause, while COMP-5 truncates based on the binary field size, offering greater flexibility and performance for numeric computations.
Leave a comment