Difference between COMP and COMP3
Last Updated :
11 Sep, 2024
Computer stores the data in more than one form. The Cobol language facilitates the programmer to specify the internal representation of the data according to the need. There are two internal forms available in the Cobol:
- DISPLAY: It is the default internal representation of the data. Any type of data can be specified with the DISPLAY internal representation.
- COMPUTATIONAL: Only the numeric data's can be specified with COMPUTATIONAL internal representation. There are many types of COMPUTATIONAL representation, like COMP, COMP-1 , COMP-2, COMP-3 etc.
USAGE clause is used to specify the type of internal representation. You can use any level-number for USAGE clause except 66 or 88.
Syntax:
USAGE IS {COMPUTATIONAL/COMP/DISPLAY}.
What is COMP?
Usage clause is applicable only on numerical data items. It represents the data purely in binary form. and can store the data either in half word or in full word depending on the size of the data. We can use only 9 and S during the data declaration:
- 9 is used to store declare integer variables.
- S is used to store the sign.
Advantages of COMP
- Efficiency in Computation: As specified, COMP is best suited for arithmetic processing of numeric data, thus making it possible to process numeric data rapidly.
- Storage Efficiency: The format that is used in COMP is the binary format and it can be even more efficient than other formats in situations where large number are involved.
- Support for Larger Numbers: Also, it appears that COMP can accommodate relatively ‘bigger’ numeric as compared to other formats.
Disadvantages of COMP
- Limited to Numeric Data: As for its characteristics, COMP is applicable only in working with numeric data and cannot accommodate alphanumeric data.
- Platform Dependency: The representation of the generated COMP data can even be different from one system to another, and this poses possible compatibility problems.
What is COMP3?
Usage clause is applicable only on numerical data items. It stores the data in packed decimal form. It uses one rightmost bit to store the sign irrespective of whether we have used S in PIC clause or not. The hexadecimal number C and F stores the positive sign at rightmost bit and D stores the negative sign at rightmost bit. We can use 9, S and V in PIC clause during data declaration.
V is used to store decimal point at a particular location in data item.
Advantages of COMP-3
- Precision in Storage: Still, COMP-3 results in accurate representation of both a decimal number making it essential for performing common financial computations.
- Compact Storage: Further, the COMP-3 code inflates the density of any numeric field by packing two digits into a single byte.
- Consistency Across Platforms: The major advantage of using COMP-3 format as compared to COMP is that it is less prone to the compatibility problems.
Disadvantages of COMP-3
- Slower Arithmetic Operations: Another disadvantage is that arithmetic operation on COMP-3 data takes slightly more time compare to COMP, this is because of packed decimal format they use.
- Complexity in Handling: The packed format of COMP-3 can be most inconvenient for manipulation, especially for conversion to and from other formats.
- Limited to Decimal Numbers: COMP-3 is good for decimal numbers but not for non-decimal numeric numbers.
Difference between COMP and COMP3
COMP | COMP3 |
---|
It represents the data in pure binary form. | It represents the data in packed decimal form. |
We can use only 9 and S in PIC Clause. | We can use 9 , S , V in PIC Clause. |
COMP usage stores the data in half word or in full word, depending on the size of the data. | COMP3 usage stores 1 digit in half byte (i.e. 4 bits) and a separate 1 bit is reserved for the sign, which is stored at the right side of the data. |
The memory to be occupied by the data according to the length is predefined i.e. : - 9(01) - 9(04) : 16 bits (2 bytes)
- 9(05) - 9(09) : 32 bits (4 bytes)
- S9(10) - S9(18) : 64 bits (8 bytes)
| The memory to be occupied by the data is defined by the following formula: - (length of variable + 1)/2 bytes.
Example : The memory occupied by S9(3) is: (3+1)/2 i.e. 2 bytes. |
COMP does not occupy extra space to store sign. | In COMP3 sign in compulsorily stored at right side and thus it occupies an extra space.
|
Example:
02 CompVariable PIC 9 USAGE IS COMP.
02 CompVariable1 PIC S9(5) USAGE IS COMP. | Example:
02 Variable PIC 9 USAGE IS COMP3.
02 Variable1 PIC S9(10) USAGE IS COMP3.
02 Variable2 PIC S9V99 USAGE IS COMP3. |
Conclusion
COMP and COMP-3 are two different numeric storage formats that are used in COBOL programming languages but each of both has its disadvantage and advantage. It is also important to note that the COMP is better than the COMP-3 for specific application where the speed and efficiency of the arithmetic operations is a key factor important than the decimal precision while the COMP-3 is more appropriate for such applications where decimal precision is important than the efficiency of the arithmetic operations such as finance areas. Comparing the formats makes it possible for COBOL programmers to identify which format to use depending on the applications’ needs.