These are few of the most asked and uncomfortably answered question in cobol. Let's discuss this today. I will try my best to be as simple and explanatory as possible, along with examples.
- WS-NUM PIC (5) VALUE '12345' :
This is a numeric variable in cobol. This type of variable is stored in mainframe as display or character format. For the above variable, if we read it in HEX : its stored as " F1F2F3F4F5". It will occupy 5 bytes storage.
Note: These variables are not most efficient for numerical calculations. For any type of calculation they need to converted to binary or comp-3 format for calculations. Binary is more native to the system. Binary is approx. 5-8 times faster and occupies lesser space.
- Comp or BINARY (both are synonymous):
S9(1) - S9(4) COMP is 2 byte integer (-32768 - +32767 , half word)
S9(5) - S9(9) COMP is 4 byte integer( full word) ;
S9(10) - S9(18)COMP is 8 byte integer(double word )
i.e. - 9(2) is same as 9(4); 9(5) is same as 9(9); 9(10) is same as 9(18) in terms of storage.
- COMPUTATIONAL-1 or COMP-1 : WS-NUM COMP-1
- COMPUTATIONAL-2 or COMP-2 : WS-NUM COMP-2
- COMPUTATIONAL-3 or COMP-3 (internal decimal)
Packed Decimal representation stores two decimal digits in one byte. A packed decimal representation stores decimal digits in each "nibble" of a byte. Each byte has two nibbles, and each nibble is indicated by a hexadecimal digit. For example, the value 12 would be stored in two nibbles, using the hexadecimal digits 1 and 2. The sign indication is dependent on your operating environment. On an IBM mainframe, the sign is indicated by the last nibble. The C indicates a positive value, and D indicates a negative value.
The mainframe can perform arithmetic functions on packed-decimal fields without having to convert the format. Storing numeric values in a packed-decimal format may save a significant amount of storage space. For example, on the mainframe the value 12,345 would be five (5) bytes in length (i.e. x'F1F2F3F4F5'). If the same information is stored in a packed-decimal (i.e. USAGE IS COMP-3) the field would be three (3) bytes in length (i.e. x'12345C').
Example for comp-3 size calculations :
PIC S9(7) COMP-3. Byte size = (7 + 1) / 2 = 4
PIC S9(5)V99 COMP-3. Byte size = (5 + 2 + 1) / 2 = 4
PIC S9(6) COMP-3. Byte size = (6 + 1) / 2 = 3.5, rounded to 4
Comp-3 fields reserve a nybble for the sign, even for "unsigned" values, so the following fields are still 4 bytes:
PIC 9(7) COMP-3. Byte size = (7 + 1) / 2 = 4
PIC 9(6) COMP-3. Byte size = (6 + 1) / 2 = 3.5, rounded to 4
There is an automated tool to calculate size for different comp variables in cobol. Click here
Hope you find the above post useful. Please do provide your feedback or comments, if you had any. You may also post any of your cobol queries in the comments.