COBOL USAGE (COMP, COMP-3)
The PICTURE clause says what values an item can hold. The USAGE clause says how those values are stored
in memory. The default, USAGE DISPLAY, stores one character per digit, which is readable but slow for
arithmetic. The other usages trade readability for speed and space.
| Usage | Also written | Storage | Typical use |
|---|---|---|---|
DISPLAY |
(default) | 1 byte per digit, as characters | Records, printing, data exchange |
BINARY |
COMP, COMP-4 |
Binary integer: 2, 4 or 8 bytes | Counters, subscripts, lengths |
PACKED-DECIMAL |
COMP-3 |
2 digits per byte + sign half-byte | Money in mainframe files and DB2 |
COMP-5 |
Native binary, full range of the bytes | Calling C or system APIs | |
COMP-1 / COMP-2 |
FLOAT-SHORT / FLOAT-LONG |
Floating point (4 / 8 bytes) | Scientific values (rare) |
INDEX |
Like an index | Saving index values |
Sizes in bytes
IDENTIFICATION DIVISION.
PROGRAM-ID. USAGE-SIZES.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-DISP PIC S9(7)V99 VALUE 12345.67.
01 WS-PACKED PIC S9(7)V99 COMP-3 VALUE 12345.67.
01 WS-BIN-4 PIC S9(4) COMP VALUE 1234.
01 WS-BIN-9 PIC S9(9) COMP VALUE 123456789.
01 WS-BIN-18 PIC S9(18) COMP VALUE 1.
PROCEDURE DIVISION.
DISPLAY "DISPLAY S9(7)V99: " FUNCTION LENGTH(WS-DISP)
" bytes, value " WS-DISP
DISPLAY "COMP-3 S9(7)V99: " FUNCTION LENGTH(WS-PACKED)
" bytes, value " WS-PACKED
DISPLAY "COMP S9(4): " FUNCTION LENGTH(WS-BIN-4)
" bytes"
DISPLAY "COMP S9(9): " FUNCTION LENGTH(WS-BIN-9)
" bytes"
DISPLAY "COMP S9(18): " FUNCTION LENGTH(WS-BIN-18)
" bytes"
STOP RUN.DISPLAY S9(7)V99: 9 bytes, value +0012345.67
COMP-3 S9(7)V99: 5 bytes, value +0012345.67
COMP S9(4): 2 bytes
COMP S9(9): 4 bytes
COMP S9(18): 8 bytes
DISPLAY converts every usage back to readable digits, so the values look the same; only the storage differs.
How COMP-3 (packed decimal) works
Packed decimal stores two decimal digits in each byte (one per half-byte, or "nibble") and puts the sign in the
last nibble: C for positive, D for negative, F for unsigned. +12345.67 in PIC S9(7)V99 COMP-3 is
9 digits, padded to an even count with a leading zero:
digits: 0 0 1 2 3 4 5 6 7 sign C
bytes: 00 12 34 56 7C -> 5 bytes instead of 9The size of a packed field is (digits + 1) / 2 bytes, rounded up. Mainframes have hardware instructions for
packed arithmetic, which is why so much financial data is stored this way. You can see the raw bytes with
FUNCTION HEX-OF (a GnuCOBOL extension):
IDENTIFICATION DIVISION.
PROGRAM-ID. PACKED-BYTES.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-POS PIC S9(7)V99 COMP-3 VALUE 12345.67.
01 WS-NEG PIC S9(3) COMP-3 VALUE -42.
01 WS-TEXT-NUM PIC 9(4) VALUE 1234.
PROCEDURE DIVISION.
DISPLAY "+12345.67 packed: " FUNCTION HEX-OF(WS-POS)
DISPLAY "-42 packed: " FUNCTION HEX-OF(WS-NEG)
DISPLAY "1234 as DISPLAY: " FUNCTION HEX-OF(WS-TEXT-NUM)
STOP RUN.+12345.67 packed: 001234567C
-42 packed: 042D
1234 as DISPLAY: 31323334
In DISPLAY usage the digits are the ASCII characters 31 32 33 34 (on a mainframe they would be EBCDIC
F1 F2 F3 F4).
Binary (COMP) and the PICTURE limit
A COMP item holds a binary integer, but by default COBOL still respects the number of digits in the picture.
PIC S9(4) COMP can physically hold up to 32,767, yet the standard says values beyond 9,999 are truncated. GnuCOBOL
follows that rule by default (-fbinary-truncate); IBM's TRUNC(BIN) option and COMP-5 use the full binary range:
IDENTIFICATION DIVISION.
PROGRAM-ID. BINARY-TRUNC.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-COMP PIC S9(4) COMP VALUE 9000.
01 WS-COMP5 PIC S9(4) COMP-5 VALUE 9000.
PROCEDURE DIVISION.
ADD 3000 TO WS-COMP
ADD 3000 TO WS-COMP5
DISPLAY "COMP 9000 + 3000 = " WS-COMP
DISPLAY "COMP-5 9000 + 3000 = " WS-COMP5
STOP RUN.COMP 9000 + 3000 = +2000
COMP-5 9000 + 3000 = +12000
With COMP the result 12000 lost its high-order digit and became 2000; COMP-5 kept it.
Choosing a usage
- Counters, subscripts and loop variables:
PIC S9(4) COMPorPIC S9(8) COMP(fast binary arithmetic). - Money and quantities in files and databases:
COMP-3on mainframes, orDISPLAYfor portable text files. - Anything you print or exchange as text:
DISPLAY, then edit it into a report field. - Avoid
COMP-1/COMP-2for money: they are binary floating point, with the same rounding issues asdoublein other languages.
IDENTIFICATION DIVISION.
PROGRAM-ID. FLOAT-VS-DECIMAL.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-FLOAT COMP-2 VALUE 0.
01 WS-DECIMAL PIC 9V99 VALUE 0.
01 WS-I PIC 99.
01 WS-SHOW PIC 9.9(17).
PROCEDURE DIVISION.
PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I > 10
ADD 0.1 TO WS-FLOAT
ADD 0.1 TO WS-DECIMAL
END-PERFORM
MOVE WS-FLOAT TO WS-SHOW
DISPLAY "COMP-2 ten times 0.1: " WS-SHOW
DISPLAY "PIC 9V99 ten times 0.1: " WS-DECIMAL
STOP RUN.COMP-2 ten times 0.1: 0.99999999999999977
PIC 9V99 ten times 0.1: 1.00
Test yourself
How many bytes does PIC S9(5) COMP-3 occupy?
- 2
- 3
- 5
- 6
Which usage is best for a loop counter or subscript?
- DISPLAY
- COMP (binary)
- COMP-2
- COMP-3
What is the default usage of a numeric item?
- COMP
- COMP-3
- DISPLAY
- BINARY