View previous topic :: View next topic
|
Author |
Message |
PrabakarV
New User
Joined: 21 Dec 2007 Posts: 88 Location: My Desk
|
|
|
|
Could you please tell me why i am not getting negative sign for my case 2 in the below code.
Code: |
DISPLAY 'TEST 1'.
DISPLAY ' '.
MOVE '+0005' TO CHAR-FIELD.
DISPLAY 'CHAR FIELD IS : ' CHAR-FIELD.
MOVE CHAR-FIELD TO NUM-FIELD.
DISPLAY 'MOVING X(5) TO S9(4)'.
DISPLAY 'NUM-FIELD-SIGNED IS : ' NUM-FIELD.
COMPUTE NUM-FIELD = NUM-FIELD + TEMP-VAR.
DISPLAY 'TEMP-VAR ' TEMP-VAR.
DISPLAY 'ADDING 0003 TO SIGNED FIELD'.
DISPLAY 'NUM-FIELD-SIGNED IS : ' NUM-FIELD-N.
DISPLAY ' '.
DISPLAY 'TEST 2'.
DISPLAY ' '.
MOVE '-0005' TO CHAR-FIELD.
DISPLAY 'CHAR FIELD IS : ' CHAR-FIELD.
MOVE CHAR-FIELD TO NUM-FIELD.
DISPLAY 'MOVING X(5) TO S9(4)'.
DISPLAY 'NUM-FIELD-SIGNED IS : ' NUM-FIELD.
COMPUTE NUM-FIELD = NUM-FIELD + TEMP-VAR.
DISPLAY 'TEMP-VAR ' TEMP-VAR.
DISPLAY 'ADDING 0003 TO SIGNED FIELD'.
DISPLAY 'NUM-FIELD-SIGNED IS : ' NUM-FIELD-N.
DISPLAY ' '.
DISPLAY 'TEST 3'.
DISPLAY ' '.
MOVE +0005 TO NUM-FIELD.
DISPLAY 'NUM-FIELD-SIGNED IS : ' NUM-FIELD-N.
COMPUTE NUM-FIELD = NUM-FIELD + TEMP-VAR.
DISPLAY 'TEMP-VAR ' TEMP-VAR.
DISPLAY 'ADDING 0003 TO SIGNED FIELD'.
DISPLAY 'NUM-FIELD-SIGNED IS : ' NUM-FIELD-N.
DISPLAY ' '.
DISPLAY 'TEST 4'.
DISPLAY ' '.
MOVE -0005 TO NUM-FIELD.
DISPLAY 'NUM-FIELD-SIGNED IS : ' NUM-FIELD-N.
COMPUTE NUM-FIELD = NUM-FIELD + TEMP-VAR.
DISPLAY 'TEMP-VAR ' TEMP-VAR.
DISPLAY 'ADDING 0003 TO SIGNED FIELD'.
DISPLAY 'NUM-FIELD-SIGNED IS : ' NUM-FIELD-N. |
OUTPUT that I got after running the above:
Code: |
TEST 1
CHAR FIELD IS : +0005
MOVING X(5) TO S9(4)
NUM-FIELD-SIGNED IS : 000E
TEMP-VAR 0003
ADDING 0003 TO SIGNED FIELD
NUM-FIELD-SIGNED IS : 000H
TEST 2
CHAR FIELD IS : -0005
MOVING X(5) TO S9(4)
NUM-FIELD-SIGNED IS : 000E
TEMP-VAR 0003
ADDING 0003 TO SIGNED FIELD
NUM-FIELD-SIGNED IS : 000H
TEST 3
NUM-FIELD-SIGNED IS : 000E
TEMP-VAR 0003
ADDING 0003 TO SIGNED FIELD
NUM-FIELD-SIGNED IS : 000H
TEST 4
NUM-FIELD-SIGNED IS : 000N
TEMP-VAR 0003
ADDING 0003 TO SIGNED FIELD
NUM-FIELD-SIGNED IS : 000K |
isn't that case 2 and 4 supposed to be issuing same results. Please give me suggestions on what i am missing here. |
|
Back to top |
|
|
Bill Woodger
Moderator Emeritus
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
|
|
|
|
You are moving a literal. Something in quotes. Check in the manual what this will do, and you will have your answer, or most of it. |
|
Back to top |
|
|
PrabakarV
New User
Joined: 21 Dec 2007 Posts: 88 Location: My Desk
|
|
|
|
data definition is below
Code: |
01 COMP-TEST.
05 CHAR-FIELD PIC X(5).
05 NUM-FIELD PIC S9(4).
05 TEMP-VAR PIC 9(4) VALUE 0003. |
|
|
Back to top |
|
|
Bill Woodger
Moderator Emeritus
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
|
|
|
|
Quote: |
data definition is below
|
OK.... but where is the result of your research in the manuals? You have two problems. Find the first, and you can get help with the second. |
|
Back to top |
|
|
PrabakarV
New User
Joined: 21 Dec 2007 Posts: 88 Location: My Desk
|
|
|
|
I think you are referring to this item below; (under VS COBOL ref 3.22.1)
Quote: |
If the sending item has an operational sign, the unsigned value is
used. If the operational sign occupies a separate character, that
character is not moved, and the size of the sending item is considered
to be 1 less character than the actual size. |
|
|
Back to top |
|
|
Bill Woodger
Moderator Emeritus
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
|
|
|
|
No, not what I was thinking of. You are moving a literal. That literal happens to contain "-", which the Cobol compiler cares about neither one way nor the other when you move that literal to a numeric field. Forget, for the moment, that there is a sign in the literal, cos, between you and the compiler, only you know that.
I'm hoping that you can locate what happens when you move a non-numeric literal to a numeric field. Because that, plus a little extra, is what is happening in your example. |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8696 Location: Dubuque, Iowa, USA
|
|
|
|
Your test 2 and test 4 are very different. COBOL treats any literal -- something with quote or tic marks around it -- as an unsigned positive value. Your putting a negative sign in the literal does not change that treatment. That negative sign is completely ignored. Go back and look at your results again, considering this new information. |
|
Back to top |
|
|
PrabakarV
New User
Joined: 21 Dec 2007 Posts: 88 Location: My Desk
|
|
|
|
Got answer for it.
Quote: |
The following rules outline the execution of valid elementary moves. When the receiving item is:
Numeric or Numeric-edited:
° If the receiving item is signed, the sign of the sending item is placed in the receiving item, with any necessary sign conversion. If the sending item is unsigned, a positive operational sign is generated for the receiving item.
° If the receiving item is unsigned, the absolute value of the sending item is moved, and no operational sign is generated for the receiving item.
° When the sending item is alphanumeric, the data is moved as if the sending item were described as an unsigned integer. |
Thanks Bill, Robert. |
|
Back to top |
|
|
Bill Woodger
Moderator Emeritus
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
|
|
|
|
Yes.
When I started out, if I had a question I resolved it by looking in manual, talking to colleagues, and if still no answer, keep at the manual until I got it workd out. Didn't have internet forums. I think you should always try the first two methods first. You'll pick up a lot mroe that way, probably.
Not quite finished yet.
You might be thinking that "alphanumeric" means a number in a PIC X(x) field. It doesn't. Try putting total rubbish in there. The compiler will still do the move. Sometimes you'll get S0C7. Sometimes you won't. If you are investigating the actions of MOVE statements, then you should know. Try changing you "-" to a "¬" (logical not) and see what happens.
Quote: |
° If the receiving item is signed, the sign of the sending item is placed in the receiving item, with any necessary sign conversion. If the sending item is unsigned, a positive operational sign is generated for the receiving item.
...
° When the sending item is alphanumeric, the data is moved as if the sending item were described as an unsigned integer.
|
|
|
Back to top |
|
|
Bill Woodger
Moderator Emeritus
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
|
|
|
|
Bill Woodger wrote: |
Sometimes you'll get S0C7. |
I don't mean on the MOVE. I defy anyone to get a S0C7 moving PIC X(x) to PIC 9(x). I meant on the ADD afterwards. |
|
Back to top |
|
|
|