When to use COMP,COMP-3 variables?

OS/VS COBOL, COBOL II, Enterprise COBOL for z/OS. OpenCOBOL and OOCobol.
KavalJeet
Registered Member
Posts: 48
Joined: Wed Jun 19, 2013 9:47 am

When to use COMP,COMP-3 variables?

Post by KavalJeet »

Hi ,

I'm not sure what answer is best suitable when someone asks :: When should we go for COMP,COMP-3 variables? What advantage these variables have over Usage DISPLAY variables other than Memory size?
User avatar
Robert Sample
Global Moderator
Global Moderator
Posts: 1905
Joined: Fri Jun 28, 2013 1:22 am
Location: Dubuque Iowa

Re: When to use COMP,COMP-3 variables?

Post by Robert Sample »

COMP and COMP-3 variables are best when doing arithmetic with the variables. COMP works better for indexing / subscripting while COMP-3 is typically how COBOL does mathematical operations. However, the difference between these variables and zoned decimal variables will be negligible unless you are doing BILLIONS of operations in your program.
KavalJeet
Registered Member
Posts: 48
Joined: Wed Jun 19, 2013 9:47 am

Re: When to use COMP,COMP-3 variables?

Post by KavalJeet »

Robert Sample wrote: However, the difference between these variables and zoned decimal variables will be negligible unless you are doing BILLIONS of operations in your program.
What difference you're talking about here?
User avatar
Robert Sample
Global Moderator
Global Moderator
Posts: 1905
Joined: Fri Jun 28, 2013 1:22 am
Location: Dubuque Iowa

Re: When to use COMP,COMP-3 variables?

Post by Robert Sample »

Using COMP variables for subscripts and COMP-3 variables for arithmetic is SLIGHTLY more efficient (less CPU time) than zoned decimal. However, to see any CPU difference in COMP / COMP-3 versus zoned decimal, you have to be doing a LOT of calculations / subscripts. In general, unless you have a known issue with performance, you could code your program without any COMP / COMP-3 variables and you would not see any difference in CPU time.
KavalJeet
Registered Member
Posts: 48
Joined: Wed Jun 19, 2013 9:47 am

Re: When to use COMP,COMP-3 variables?

Post by KavalJeet »

Thanks you Robert.

In a recent interview I told the same to the interviewer but he asked leave it and in general how the "amount filed" will be defined comp or comp-3... :?
User avatar
Robert Sample
Global Moderator
Global Moderator
Posts: 1905
Joined: Fri Jun 28, 2013 1:22 am
Location: Dubuque Iowa

Re: When to use COMP,COMP-3 variables?

Post by Robert Sample »

An amount field implies accumulation and / or arithmetic so use COMP-3.
Post Reply Previous topicNext topic

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.”