Page 1 of 1

Moving Alphanumeric to COMP

PostPosted: Tue Oct 13, 2015 11:13 am
by danny_ce16
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.

Re: Moving Alphanumeric to COMP

PostPosted: Tue Oct 13, 2015 12:17 pm
by enrico-sorichetti
s9(9) defines a number with 9 digits,
why would You expect that it should contain 10 ( x10 ) ?

Re: Moving Alphanumeric to COMP

PostPosted: Tue Oct 13, 2015 3:43 pm
by BillyBoyo
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.

Re: Moving Alphanumeric to COMP

PostPosted: Tue Oct 13, 2015 6:59 pm
by danny_ce16
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.

Re: Moving Alphanumeric to COMP

PostPosted: Tue Oct 13, 2015 9:16 pm
by BillyBoyo
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.