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-5 can store values from -32768 to +32767
  • PIC 9 COMP-5 and PIC 999 COMP-5 can store values from 0 to 65535

COMP vs COMP-5: Key Differences

Picture ClauseCOMP RangeCOMP-5 Range
PIC 90 to 90 to 65535
PIC S99-99 to +99-32768 to +32767
PIC 9990 to 9990 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

Discover more from DBzTech-Technology Dossier

Subscribe now to keep reading and get access to the full archive.

Continue reading