USAGE COMP-3 and packed decimals
COMP-3 is the third way to store a number, and on a mainframe file it is the usual one. Two digits share a byte, half a byte each, and the last half byte holds the sign. It is neither text nor binary: it is the decimal digits packed, and it is what IBM calls packed decimal.
Two digits in a byte
A byte has two halves of four bits, and four bits hold one decimal digit from zero to nine with room to spare. Packed decimal uses the spare room for nothing and puts a second digit where it is: twelve thousand three hundred and forty-five and sixty-seven hundredths is seven digits, so it is four bytes, and the last half byte is the sign.
IDENTIFICATION DIVISION.
PROGRAM-ID. PACKED.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 D-AMT PIC S9(5)V99 VALUE 12345.67.
01 C-AMT PIC S9(5)V99 COMP VALUE 12345.67.
01 P-AMT PIC S9(5)V99 COMP-3 VALUE 12345.67.
PROCEDURE DIVISION.
DISPLAY "DISPLAY " LENGTH OF D-AMT " hex " FUNCTION HEX-OF(D-AMT).
DISPLAY "COMP " LENGTH OF C-AMT " hex " FUNCTION HEX-OF(C-AMT).
DISPLAY "COMP-3 " LENGTH OF P-AMT " hex " FUNCTION HEX-OF(P-AMT).
DISPLAY "COMP-3 value [" P-AMT "]".
STOP RUN.
cobc -x -free -Wall -o packed packed.cob
./packed
DISPLAY 7 hex 31323334353637
COMP 4 hex 0012D687
COMP-3 4 hex 1234567C
COMP-3 value [+12345.67]
The DISPLAY field is seven characters and its hex is seven ASCII codes, 31 for 1 and so on. The COMP field is a binary integer, 0012D687, which is 1234567 in hex. The COMP-3 field is the digits themselves, 1234567, followed by C, the sign. Both binary forms are four bytes; the packed one is the only one whose bytes can be read as digits.
The sign nibble
The half byte after the last digit is a sign, and it has three common values: C for a positive number, D for a negative one, and F for a field whose picture has no S.
IDENTIFICATION DIVISION.
PROGRAM-ID. NIBBLES.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 P-POS PIC S9(3) COMP-3 VALUE 42.
01 P-NEG PIC S9(3) COMP-3 VALUE -42.
01 P-ZER PIC S9(3) COMP-3 VALUE 0.
01 U-POS PIC 9(3) COMP-3 VALUE 42.
PROCEDURE DIVISION.
DISPLAY "+42 [" FUNCTION HEX-OF(P-POS) "]".
DISPLAY "-42 [" FUNCTION HEX-OF(P-NEG) "]".
DISPLAY " 0 [" FUNCTION HEX-OF(P-ZER) "]".
DISPLAY "unsigned 42 [" FUNCTION HEX-OF(U-POS) "]".
STOP RUN.
cobc -x -free -Wall -o nibbles nibbles.cob
./nibbles
+42 [042C]
-42 [042D]
0 [000C]
unsigned 42 [042F]
The three digits 042 plus a sign make four half bytes and the field is two bytes. The unsigned field is also two bytes, with F where the sign would be, so a COMP-3 picture spends the half byte whether or not it has an S. Dropping the S changes what the program may store and not how long the field is.
The C and D are not arbitrary: they are the IBM convention, and the same values appear in a DB2 table, an IMS segment and a VSAM record. A program that writes packed decimal writes something another platform can read without a conversion table, which is why the format outlived the machines it was designed on.
How long a packed field is
The rule is one line. Take the number of digits of the picture, add one for the sign, divide by two and round up. Seven pictures, seven lengths:
IDENTIFICATION DIVISION.
PROGRAM-ID. LENS.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC 9(3) COMP-3.
01 B PIC S9(3) COMP-3.
01 C PIC 9(4) COMP-3.
01 D PIC S9(5)V99 COMP-3.
01 E PIC S9(9) COMP-3.
01 F PIC S9(10) COMP-3.
01 G PIC S9(18) COMP-3.
01 H PIC 9(3) COMP.
PROCEDURE DIVISION.
DISPLAY "9(3) " LENGTH OF A.
DISPLAY "S9(3) " LENGTH OF B.
DISPLAY "9(4) " LENGTH OF C.
DISPLAY "S9(5)V99 " LENGTH OF D.
DISPLAY "S9(9) " LENGTH OF E.
DISPLAY "S9(10) " LENGTH OF F.
DISPLAY "S9(18) " LENGTH OF G.
DISPLAY "9(3) COMP " LENGTH OF H.
STOP RUN.
cobc -x -free -Wall -o lens lens.cob
./lens
9(3) 2
S9(3) 2
9(4) 3
S9(5)V99 4
S9(9) 5
S9(10) 6
S9(18) 10
9(3) COMP 2
A 9(4) is three bytes because four digits and a sign are five half bytes, and the odd one is filled with a zero. S9(18) is ten bytes: eighteen digits and a sign are nineteen half bytes, and that is the largest packed field COBOL defines. Nine digits take five bytes here and four in COMP, so packed decimal is not the smallest form at every size: its case is a decimal number that has to stay exact in a record another platform reads.
Money in packed decimal
The reason to keep an amount in COMP-3 is that the cents are digits in the field and not a binary fraction that has to be rounded back. An addition of four amounts is the same addition on paper:
IDENTIFICATION DIVISION.
PROGRAM-ID. MONEY.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 TOTAL-F PIC S9(7)V99 COMP-3 VALUE 0.
01 ITEM-F PIC S9(5)V99 COMP-3 VALUE 1234.56.
01 CENTS-F PIC S9(3)V99 COMP-3 VALUE 0.10.
PROCEDURE DIVISION.
ADD ITEM-F TO TOTAL-F.
ADD ITEM-F TO TOTAL-F.
ADD CENTS-F TO TOTAL-F.
ADD 0.20 TO TOTAL-F.
DISPLAY "total [" TOTAL-F "]".
MOVE 1.239 TO CENTS-F.
DISPLAY "after MOVE 1.239 [" CENTS-F "]".
STOP RUN.
cobc -x -free -Wall -o money money.cob
./money
money.cob:16: warning: value size exceeds data size [-Wtruncate]
14 | ADD 0.20 TO TOTAL-F.
15 | DISPLAY "total [" TOTAL-F "]".
16 > MOVE 1.239 TO CENTS-F.
17 | DISPLAY "after MOVE 1.239 [" CENTS-F "]".
18 | STOP RUN.
money.cob:16: note: value is 1239 [-Wtruncate]
money.cob:8: note: 'CENTS-F' defined here as PIC S9(3)V99 [-Wtruncate]
6 | 01 TOTAL-F PIC S9(7)V99 COMP-3 VALUE 0.
7 | 01 ITEM-F PIC S9(5)V99 COMP-3 VALUE 1234.56.
8 > 01 CENTS-F PIC S9(3)V99 COMP-3 VALUE 0.10.
9 |
10 | PROCEDURE DIVISION.
total [+0002469.42]
after MOVE 1.239 [+001.23]
1234.56 twice is 2469.12, then ten and twenty cents give 2469.42, and the total is exact to the cent because no step lost a digit. The second result shows the other side of the same rule: a third decimal place has nowhere to go, so it is cut and the build warns. A field that must keep three places needs a picture with three, and a value that must be rounded needs ROUNDED, which its own lesson takes.
Which usage to choose
- DISPLAY when the bytes have to be readable without a program: a file for another system, a line of a report, a field an operator sees.
- COMP for counters, subscripts and dates, where the value is small and the arithmetic is integer.
- COMP-3 for money and for any decimal number with many digits that has to stay exact and take little room, especially in a record that goes to a mainframe.
- COMP-5 only at an interface to native code, as the lesson before this one shows.
A comparison makes the trade concrete. Ten amounts of S9(7)V99 take seventy bytes in DISPLAY and fifty in COMP-3, and both hold the same cents exactly. A record of a hundred such amounts saves two kilobytes per copy, which on a tape archive of a million records is the difference between two tapes and four.
Exercises
- Print LENGTH OF a PIC S9(7)V99 COMP-3 and check the answer with the rule of this lesson.
- Print the hex of PIC S9(4) COMP-3 VALUE -1234. How many bytes, and which half byte is the sign?
- Store 999.99 in a PIC S9(3)V99 COMP-3, add 0.01, and print the field. What does it hold, and what would ON SIZE ERROR have done?
- Move a five-digit number into a PIC S9(4) COMP-3 and read the warning, then do the same into a PIC 9(4) COMP-5 and compare what happens.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
How many decimal digits does one byte of a COMP-3 field hold?
Answer
Two, and the last half byte of the field is the sign
A nibble holds one digit, so a byte holds two; the field ends with one more nibble for the sign.
-
How long is a
PIC S9(5)V99 COMP-3field?Answer
4 bytes
Seven digits and a sign are eight half bytes, which is four bytes exactly.
-
Which half byte ends a negative COMP-3 number?
Answer
D
C marks a positive value, D a negative one and F a field whose picture has no S; the digits themselves are 0 to 9.
-
A programmer drops the S from a COMP-3 picture to save a byte. What happens?
Answer
The field is the same length: the sign half byte is spent anyway
An unsigned packed field ends with F rather than C or D, but it still ends with a sign half byte, so 9(3) and S9(3) are both two bytes.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q1. DATA DIVISION. WORKING-STORAGE SECTION. 01 P-NEG PIC S9(3) COMP-3 VALUE -42. PROCEDURE DIVISION. DISPLAY FUNCTION HEX-OF(P-NEG). STOP RUN.Answer
042D
The digits are 042 and the last half byte is D for a negative value.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q2. DATA DIVISION. WORKING-STORAGE SECTION. 01 U-POS PIC 9(3) COMP-3 VALUE 42. PROCEDURE DIVISION. DISPLAY LENGTH OF U-POS. STOP RUN.Answer
2
Three digits and the sign half byte are four nibbles, two bytes; the lack of an S does not remove the nibble.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q3. DATA DIVISION. WORKING-STORAGE SECTION. 01 P-AMT PIC S9(7)V99 COMP-3. PROCEDURE DIVISION. DISPLAY LENGTH OF P-AMT. STOP RUN.Answer
5
Nine digits and a sign are ten half bytes, which is five bytes.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q4. DATA DIVISION. WORKING-STORAGE SECTION. 01 TOTAL-F PIC S9(5)V99 COMP-3 VALUE 0. PROCEDURE DIVISION. ADD 0.10 TO TOTAL-F. ADD 0.20 TO TOTAL-F. DISPLAY "[" TOTAL-F "]". STOP RUN.Answer
[+00000.30]
Packed decimal adds the cents exactly and the picture has five integer digits, so thirty hundredths prints with the five zeros.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 P-AMT PIC S9(5)V99 COMP-3 VALUE 12345.67. PROCEDURE DIVISION. DISPLAY FUNCTION HEX-OF(P-AMT). STOP RUN.Answer
1234567C
Packed decimal stores the seven digits and the C of a positive value; the binary and character forms of the same number are the other options.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 CENTS-F PIC S9(3)V99 COMP-3. PROCEDURE DIVISION. MOVE 1.239 TO CENTS-F. DISPLAY "[" CENTS-F "]". STOP RUN.Answer
[+001.23]
The picture has two decimal places, so the third digit is cut rather than rounded, and the build warns about the truncation.
The code on this page was built with cobc (GnuCOBOL) 3.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev