Tuesday, January 6, 2009

Cobol numeric & computational data types

What are different type of COMP fields in COBOl ? What is COMP ? What is COMP-1 ? What is COMP-3 ?

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):
COMP is binary integer data. For binary numbers, 8 bits or 1 byte, will store unsigned values from 0 to 255 or signed values from -128 to +127.
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
These are specified for internal floating-point items (single precision). COMP-1 items are 4 bytes long. The sign is contained in the first bit of the leftmost byte and the exponent is contained in the remaining 7 bits. The last 3 bytes contain the mantissa.

  • COMPUTATIONAL-2 or COMP-2 : WS-NUM COMP-2
These are specified for internal floating-point items (double precision). COMP-2 items are 8 bytes long. The sign is contained in the first bit of the leftmost byte and the remaining 7 bits contain the exponent. The remaining 7 bytes contain the mantissa.

  • COMPUTATIONAL-3 or COMP-3 (internal decimal)
This is the equivalent to PACKED-DECIMAL. Comp-3 stores data in a BCD "binary coded decimal" format with the sign after the least significant digit. This format is more storage and CPU efficient in case of numerical calcualtions.
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.

6 comments:

Arun Suri said...

can a comp variable be defined without a sign byte ?
what will be value when -7 will be
moved to A.
A pic 9(1) comp.

Kaps said...

Q)Can a comp variable be defined without a sign byte ?

A) Yes, UNSIGNED COMP variables are allowed
Example: WS-VAR PIC 9(2) COMP.

Q)what will be value when
WS-A PIC 9(1) COMP.
MOVE -7 to WS-A.

A) WS-A should hav value 7.

Murali said...

Hi,

Please answer to this question.

Why can't we define table at 01 Level?

Kaps said...

Murali,

There are some rules made to be followed, Cobol occurs clause cannot be used at 01 level. This is one of those "to be followed rules". These rules do not have any specific reason.

I would have appreciated this if this comment was posted on Subscript and index post.

Thanks,
-Kapil.

Arunam said...

I am with Kapil,

There are specific rule defined in cobol and we have stick to them.

One of the major reason why occurs clause is not allowed at 01 level is due to implicit redefine.


There are 2 types of redefines in the system ..
1. Implicit redefines : System redefines the storages system, as per the requirement ... i.e., it redefines the records defined on 01level.

When it redefines the existing data ..is system dependent ...(i dont have much data reg. this)

2.Explicit redefine : Programmers choice using redefines keyword.

Kaps said...

Thanks for your comments Maruthi