Moving Alphanumeric to COMP



Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

Moving Alphanumeric to COMP

Postby danny_ce16 » Tue Oct 13, 2015 11:13 am

Hi

My input field is X(10) and im trying to move it to a COMP field of S9(9).

I/P
0444444444
1444444444
2444444444
9777778888

In output COMP field, its getting written as
0444444444
1444444444
1850522852
1187844296

When it works fine for input starting with 0 and 1, its writing random values for others. i tried few options like func NUMVAL, writing the input to numeric first then moving it to COMP etc, but none worked. Please help me how to move the correct values to S9(9) COMP field.
danny_ce16
 
Posts: 15
Joined: Mon Oct 24, 2011 6:14 pm
Has thanked: 1 time
Been thanked: 0 time

Re: Moving Alphanumeric to COMP

 

Re: Moving Alphanumeric to COMP

Postby enrico-sorichetti » Tue Oct 13, 2015 12:17 pm

s9(9) defines a number with 9 digits,
why would You expect that it should contain 10 ( x10 ) ?
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
enrico-sorichetti
Global moderator
 
Posts: 2643
Joined: Fri Apr 18, 2008 11:25 pm
Has thanked: 0 time
Been thanked: 130 times

Re: Moving Alphanumeric to COMP

Postby BillyBoyo » Tue Oct 13, 2015 3:43 pm

enrico, you didn't count the digits in the output :-)

danny_ce16,

enrico is generally correct. If you define nine digits for a receiving field, and then try to stuff 10 digits into it, you shouldn't be surprised.

When defining a "binary" field in COBOL, the PICture limits the value which can be contained, not the size of the field.

COMP PIC S9(9) is a four-byte binary.

If you consider the binary value that can contain as a maximum, you'll see that all value 0-999,999,999 can be represented, but not all values 0-9,999,999,999. You could have some 10-digit numbers, but not all.

Enterprise COBOL has four binary definitions: BINARY, COMP, COMP-4 and COMP-5. The first three are identical (to the compiler, BINARY and COMP-4 are aliases of COMP). COMP-5 is different, it is "native binary", which means that you can use all the bit-settings available.

You have used a signed field, so you halve the possible positive values. But if you do the calculations your last two results are exaplained.

Ah. But you didn't use COMP-5, you say? Well, you did, because compiler option TRUNC(BIN) has the effect of making all binary definitions into COMP-5.

TRUNC(BIN) is presumably your site default. Which usually means your site is a small one, because there is a performance impact in using TRUNC(BIN) or COMP-5.

However, you can't just change your compiler options which generate code, as the programs will perform differently from before.

If you were to compile with TRUNC(STD) you'd see more normal-looking truncation of your values.

If you were to compile with TRUNC(OPT), you'd be breaking your contract with the compiler. The contract is never put values beyond the range of the PICture with TRUNC(OPT) else you may get unexpected results.

These users thanked the author BillyBoyo for the post:
danny_ce16 (Wed Oct 14, 2015 4:39 pm)
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 264 times

Re: Moving Alphanumeric to COMP

Postby danny_ce16 » Tue Oct 13, 2015 6:59 pm

Thanks Enrico and Billy.

You were right Billy, the maximum value that COMP S9(9) is 2,147,483,647 ; so till 1444444444 was copied but 2444444444 was beyond the limit.
danny_ce16
 
Posts: 15
Joined: Mon Oct 24, 2011 6:14 pm
Has thanked: 1 time
Been thanked: 0 time

Re: Moving Alphanumeric to COMP

Postby BillyBoyo » Tue Oct 13, 2015 9:16 pm

That's the maximum value for COMP-5.

Since you are using TRUNC(BIN), you have that maximum value for COMP and COMP-4 and BINARY.

Otherwise, the maximum value for COMP PIC S9(9) is 999999999 and the minimum is -999999999.
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 264 times


Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post