Page 1 of 1

Which variable type to choose among COMP, COMP-1, COMP-2 and COMP-3?

Posted: Tue Sep 06, 2016 8:08 pm
by Simmrith Kaur
Hi,

I'm new to COBOL. Though I have written some programs. Most of them are enhancing an existing code, so it's easy to clone some new functionality. But when we write a new COBOL program, how to decide when to go for which variable type among COMP, COMP-1, COMP-2 and COMP-3? How do a final call on whether a varaible should be declared as COMP, COMP-1, COMP-2, COMP-3? What is the basis? Please help to understand this.


Any input is greatly appreciated.

Re: Which variable type to choose among COMP, COMP-1, COMP-2 and COMP-3?

Posted: Tue Sep 06, 2016 8:38 pm
by Robert Sample
COMP-1 and COMP-2 should only be used when you need floating-point values. They have limited precision and will not yield accurate results in some cases, so they tend to be less used in business programs (except for financial calculations).

COMP-3 variable are typically used for arithmetic calculations. COMP (or COMP-5) variables are typically used for subscripts. However there are many programs that use USAGE DISPLAY variable for arithmetic and subscripts; these programs are not wrong but they do incur additional overhead to convert the values to packed decimal or binary before use, and conversion back to USAGE DISPLAY after use. Unless there is a known performance issue, the efficiency of the various data types will not have much of an impact to a program.

Re: Which variable type to choose among COMP, COMP-1, COMP-2 and COMP-3?

Posted: Tue Sep 06, 2016 8:39 pm
by Akatsukami
A COMP (COMP-4 and BINARY are synonyms) variable is binary (or hexadecimal, if you like), but further constrained by the decimal picture; a field with usage and picture S9(7) occupies a word (4 bytes), but will only accept numbers between (decimal) -9,999,999 and +9,999,999. The sign is the high-order bit; 0 is positive and 1 is negative. It can be used computationally, but should never be given a radix point.

COMP-1 is single-precision (word) floating-point and COMP-2 is double-precision (doubleword, 8 bytes) floating-point. They should be used only for scientific calculations (in which case you should use another language than COBOL).

COMP-3 is packed decimal; each decimal digit occupies a nybble (a half byte, 4 bits); the high-order nybble contains the sign; X'C' is positive and X'D' is negative. X'F' is unsigned, but treated as positive in arithmetic calculations. It can be used computationally.

Re: Which variable type to choose among COMP, COMP-1, COMP-2 and COMP-3?

Posted: Thu Sep 08, 2016 3:42 pm
by Simmrith Kaur
Robert Sample wrote: COMP-1 and COMP-2 should only be used when you need floating-point values. They have limited precision and will not yield accurate results in some cases, so they tend to be less used in business programs (except for financial calculations).

COMP-3 variable are typically used for arithmetic calculations. COMP (or COMP-5) variables are typically used for subscripts. However there are many programs that use USAGE DISPLAY variable for arithmetic and subscripts; these programs are not wrong but they do incur additional overhead to convert the values to packed decimal or binary before use, and conversion back to USAGE DISPLAY after use. Unless there is a known performance issue, the efficiency of the various data types will not have much of an impact to a program.
COBOL is a language for businesses so when you mention that there can be "limited precision" I wonder why such a flaw exist in this language? :?

Thanks on the explnation of COMP and COMP-5. But most of the old program make use of only COMP for subscript but not of COMP-5? Is it because of some better functionality of COMP over COMP-5?

Re: Which variable type to choose among COMP, COMP-1, COMP-2 and COMP-3?

Posted: Thu Sep 08, 2016 3:47 pm
by Simmrith Kaur
Akatsukami wrote: COMP-1 is single-precision (word) floating-point and COMP-2 is double-precision (doubleword, 8 bytes) floating-point. They should be used only for scientific calculations (in which case you should use another language than COBOL).
But are these variables will be understood by other languages?

What is radix point?

Re: Which variable type to choose among COMP, COMP-1, COMP-2 and COMP-3?

Posted: Thu Sep 08, 2016 5:34 pm
by nicc
But are these variables will be understood by other languages?
What do you mean? Do other languages have similar datatypes or will other languages understand the data of these datatypes?
Have you looked at other languages to see what data types they have? Have you googled to see if there is a cross-reference between datatypes of one language and another?

Use google to disover about "Radix point".

Re: Which variable type to choose among COMP, COMP-1, COMP-2 and COMP-3?

Posted: Thu Sep 08, 2016 5:38 pm
by enrico-sorichetti
COBOL and PL/1 will understand ALSO numbers represented in packed and zoned decimal format

all the rest will probably understand only floating and binary representations

Re: Which variable type to choose among COMP, COMP-1, COMP-2 and COMP-3?

Posted: Thu Sep 08, 2016 5:38 pm
by Robert Sample
I wonder why such a flaw exist in this language?
It is not a flaw in the language, it is the way floating point works. You have only 24 bits in COMP-1 and 56 bits in COMP-2 to represent the value. An 18-byte zoned decimal variable has 144 bits to represent the value; if using ARITH(EXTEND) you have 248 bits to represent the value. Even with half the bits being zone bits, you still have 72 to 124 bits for the values. Floating point values are available in almost all mainframe languages, so they are understood by them.

COMP-5 has not always been available to COBOL -- I believe it was added in the 1985 language update. Prior to that, COMP was all that was available, hence many older programs do not reference COMP-5. While handy, it can be disconcerting for a new programmer to define a 4-byte numeric variable and have a 5-byte value show up in it.

Googling radix point will give you the Wikipedia definition; there is no reason to post such a question when 15 seconds with a browser gives you the answer. However, for completeness, a radix point is the separator between the integer and fractional parts of a number -- a period in the US, a comma in Europe. It is often called a decimal point in elementary school classes in the US; I'm not sure what they call the comma in European classes.