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

OS/VS COBOL, COBOL II, Enterprise COBOL for z/OS. OpenCOBOL and OOCobol.
Post Reply
Simmrith Kaur
New Member
Posts: 4
Joined: Mon Jan 18, 2016 10:44 am

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

Post 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.
User avatar
Robert Sample
Global Moderator
Global Moderator
Posts: 1891
Joined: Fri Jun 28, 2013 1:22 am
Location: Dubuque Iowa
United States of America

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

Post 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.
User avatar
Akatsukami
Global Moderator
Global Moderator
Posts: 122
Joined: Tue Oct 20, 2015 3:20 am
Location: Bloomington, IL
Contact:

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

Post 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.
"I come to the conclusion that, men loving according to their own will and fearing according to that of the prince, a wise prince should establish himself on that which is in his own control and not in that of others." -- Niccolò Machiavelli
Simmrith Kaur
New Member
Posts: 4
Joined: Mon Jan 18, 2016 10:44 am

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

Post 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?
Simmrith Kaur
New Member
Posts: 4
Joined: Mon Jan 18, 2016 10:44 am

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

Post 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?
nicc
Global Moderator
Global Moderator
Posts: 691
Joined: Wed Apr 23, 2014 8:45 pm

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

Post 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".
Regards
Nic
enrico-sorichetti
Global Moderator
Global Moderator
Posts: 826
Joined: Wed Sep 11, 2013 3:57 pm

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

Post 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
cheers
enrico
When I tell somebody to RTFM or STFW I usually have the page open in another tab/window of my browser,
so that I am sure that the information requested can be reached with a very small effort 8-)
User avatar
Robert Sample
Global Moderator
Global Moderator
Posts: 1891
Joined: Fri Jun 28, 2013 1:22 am
Location: Dubuque Iowa
United States of America

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

Post 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.
Post Reply

Create an account or sign in to join the discussion

You need to be a member in order to post a reply

Create an account

Not a member? register to join our community
Members can start their own topics & subscribe to topics
It’s free and only takes a minute

Register

Sign in

Return to “IBM COBOL, GnuCOBOL (OpenCOBOL), OOCobol.”