X type variable (REDEFINGED AS digit, with 'SIGN IS TRAILING SEPARATE') changed after execute "IF xxx NUMERIC"

68 Views Asked by At

I'm working on opencoobol1.1, and I encountered the weird problem:

Here is the Cobol code:

03 WS-INP              PIC X(30).                     
03 WS-INP-D REDEFINES WS-INP.                         
   05 WS-AMT1  PIC S9(6)V99 SIGN IS TRAILING SEPARATE.


MOVE 'QQQQQQQQQQQQ' TO WS-INP
DISPLAY WS-INP                   
IF WS-AMT1 NOT NUMERIC       
   DISPLAY WS-AMT1 
END-IF
DISPLAY WS-INP  

After the code executed, the value of WS-INP changed to 'QQQQQQQQQQ-Q'! It's supported to be keep the same value : 'QQQQQQQQQQQQ'.

So, what's wrong with the code? I found out that it's the 'IF xxx NUMERIC' sentence , aka 'IF WS-AMT1 NOT NUMERIC', caused the problem, and the type of WS-AMT1 , aka ' SIGN IS TRAILING SEPARATE'.

If the type is not ' SIGN IS TRAILING SEPARATE', everything is ok. But what is wrong with the ' SIGN IS TRAILING SEPARATE' ?

After remove ' SIGN IS TRAILING SEPARATE', things goes works.

1

There are 1 best solutions below

5
not2savvy On

I suspect that his statement changes the field contents:

IF WS-AMT1 NOT NUMERIC

Since WS-AMT1 is defined to be a signed number, when checking for a sign in the last value, this statement might cause the value of the last byte to be "normalized", which means that the first half-byte is set to x'C' (for positive). This will eventually cause weird character to appear when displaying the field as a valid number.

Note that this depends on how exactly the compiler works, and probably also on the compiler options used. I don't know opencobol, so I have to make assumptions.

I suppose you actually wanted to display the character field if the field is not numeric, perhaps like so

IF WS-AMT1 NOT NUMERIC       
   DISPLAY WS-INP
ELSE
   DISPLAY WS-INP 
END-IF