USAGE and COMPUSAGE and COMP: USAGE CLAUSE -There are two general forms of internal representation – computational and display (Usage and Comp). Only numeric data items can be displayed as computational, and as the name suggests, an item specified as computational can take part in arithmetic operations more efficiently.  Whether a data item is computational or display, it can be specified with a usage clause in addition to the PIC clause. On the other hand, any data item can be specified as display. Usage clause specifies the operating system in which the format data is stored. It cannot be used with level numbers 66 or 88. If usage clause is specified on a group, then all the elementary items will have the same usage clause. The different options available with Usage clause are as follows:

USAGE and COMP

USAGE IS DISPLAY

This is the most common form of internal data. The data item is stored in ASCII format and each character will take 1 byte. It is default usage and a data item is stored in a couple of contiguous bytes. The following example calculates the number of bytes required:

   01 WS-NUM PIC S9(5)V9(3) USAGE IS DISPLAY.
   It requires 8 bytes as sign and decimal doesn't require any byte.
   01 WS-NUM PIC 9(5) USAGE IS DISPLAY.
   It requires 5 bytes as sign.

COMPUTATIONAL / COMP

Data item is stored in binary format. Here, data items must be integer. While allocating the space, for COMP fields, the compiler will allocate space in multiples of word, that is, it can be halfword (2 bytes), full word or word (4 bytes), double word (8 bytes)

The following example calculates the number of bytes required:

   01 WS-NUM PIC S9(n) USAGE IS COMP.
   If 'n' = 1 to 4, it takes 2 bytes.
   If 'n' = 5 to 9, it takes 4 bytes.
   If 'n' = 10 to 18, it takes 8 bytes.

COMP-1

The data item is similar to Real or Float and is represented as a single precision floating point number. Internally, data is stored in hexadecimal format. COMP-1 does not accept PIC clause. Here 1 word is equal to 4 bytes. This representation is suitable for arithmetic operations.

COMP-2

Data item is similar to Long or Double and is represented as double precision floating point number. Internally, data is stored in hexadecimal format. COMP-2 does not specify PIC clause. Here 2 word is equal to 8 bytes.

COMP-3

COMP-3 enables the computer to store two digits in each storage position, except for the rightmost position, which holds the sign. The sign is stored separately as the rightmost half-a-byte regardless of whether S is specified in the PIC or not. Data item is stored in packed decimal format. Each digit occupies half a byte (1 nibble) and the sign is stored at the rightmost nibble. The following example calculates the number of bytes required:

The hexadecimal number C or F denotes a positive sign and the hexadecimal number D denotes a negative sign.

Suppose if you move 1234567 into a field defined 9(7). In DISPLAY mode, which is the default, this field will use 7 storage positions. If you define the field with PIC 9(7) COMP-3, it will, however, use only four positions

           12   34   56   7+

We can save a significant amount of storage by using the USAGE-COMP-3

01 WS-NUM PIC 9(n) USAGE IS COMP.
Number of bytes = n/2 (If n is even)
Number of bytes = n/2 + 1(If n is odd, consider only integer part)
01 WS-NUM PIC 9(4) USAGE IS COMP-3 VALUE 21.
It requires 2 bytes of storage as each digit occupies half a byte.
01 WS-NUM PIC 9(5) USAGE IS COMP-3 VALUE 21.
It requires 3 bytes of storage as each digit occupies half a byte.

TIPS (Usage and Comp):

  •     Don’t use COMP if the data item is
  •     Properly DISPLAY.
  •     Nonnumeric or edited.
  •     A field associated with a unit record device or terminal (this means, you can’t accept from the user, an item that is declared as COMP).

Use COMP if the data item is a

  •      A numeric field in a record input from a tape or disk file whether it was already COMP.
  •      Numeric is used in calculations and is a field in a record to be output to a tape or disk file from which it will later be input and used in the further computation.
  •      In WORKING-STORAGE, and is numeric and not edited and used in calculations.

Performance Improvement:
Usage Display

If you will use Usage Display data items for computation then the additional overhead is required to convert the data item to the proper type both before and after the computation.

PACKED-DECIMAL (COMP-3)

While using COMP-3 for computation then use 15 or fewer digits in PIC clause to avoid the use of library routines for multiplication and division. Using a signed data item with an odd number of digits produces more efficient code since this uses an integral multiple of bytes in storage for the data item.

Fixed-Point vs Floating-Point

When conversions are necessary, binary (COMP) and packed decimal (COMP-3) data with nine or fewer digits require the least amount of overhead when being converted to or from floating-point (COMP-1 or COMP-2) data.

COBOL Blog: Click Here IBM Reference: Click Here

Scroll to Top