View previous topic :: View next topic
|
Author |
Message |
Nileshkul
New User
Joined: 09 May 2016 Posts: 43 Location: India
|
|
|
|
I have 2 variables declared as numeric edited i.e. PIC -9(8).
When I compare both 2 variables even negative value is passed as more than positive value.
For example:
Temp-1 pic -9(8) value -877
Temp-2 pic -9(8) value 888
Now in IF statement temp-1 is passing as more than temp-2
Is it that comparing 2 numeric edited items us not numeric but alphanumeric including just negative sign. |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8700 Location: Dubuque, Iowa, USA
|
|
|
|
Quote: |
Is it that comparing 2 numeric edited items us not numeric but alphanumeric including just negative sign. |
Yes - numeric-edited variables are treated as alphanumeric by the compiler. The numeric values are NOT directly compared. |
|
Back to top |
|
|
sergeyken
Senior Member
Joined: 29 Apr 2008 Posts: 2145 Location: USA
|
|
|
|
Nileshkul wrote: |
I have 2 variables declared as numeric edited i.e. PIC -9(8).
When I compare both 2 variables even negative value is passed as more than positive value.
For example:
Temp-1 pic -9(8) value -877
Temp-2 pic -9(8) value 888
Now in IF statement temp-1 is passing as more than temp-2
Is it that comparing 2 numeric edited items us not numeric but alphanumeric including just negative sign. |
So called "numeric edited(?)" fields are in fact zoned decimal fields, in IBM introduced terminology.
In format PIC -9(8) the values are stored in memory as follows
Code: |
-877 = X'F0F0F0F0F0F8F7D7'
888 = X'F0F0F0F0F0F8F8C8' |
Those X-strings in COBOL are not treated according to arithmetic rules when compared, but rather as character strings - as if they were defined as PIC X(8). (When any math is required, those values need to be converted internally, usually to COMP-3; but not for comparison operation).
Hence, the X'F0F0F0F0F0F8F7D7' is greater than X'F0F0F0F0F0F8F8C8', which goes against common sense for any person who is new to mainframe internals. |
|
Back to top |
|
|
Nileshkul
New User
Joined: 09 May 2016 Posts: 43 Location: India
|
|
|
|
Thanks both for your reply and guidance.
So I will move these 2 numeric edited items simply to pic s9 as I want to compare them as numbers.
Thanks again.... |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8700 Location: Dubuque, Iowa, USA
|
|
|
|
Sergeyken, you're not quite right -- the leading minus sign is kept as a separate character with a value of '-' for negative values and space for positive values and X'60' for a minus sign compares larger than X'40' for a space. Code:
Code: |
000800 WORKING-STORAGE SECTION.
000810 01 WS-VARS.
000820 05 WS-TEMP1 PIC -9(08).
000830 05 WS-TEMP2 PIC -9(08).
001300 PROCEDURE DIVISION.
001301 MOVE -877 TO WS-TEMP1.
001302 MOVE 888 TO WS-TEMP2.
001310 IF WS-TEMP1 > WS-TEMP2
001320 DISPLAY '>' WS-TEMP1 '< IS LARGER THAN >' WS-TEMP2 '<'
001330 ELSE
001340 DISPLAY '>' WS-TEMP1 '< IS SMALLER THAN >' WS-TEMP2 '<'
001350 END-IF. |
shows results of
Code: |
>-00000877< IS LARGER THAN > 00000888< |
PIC S9(08) would be as you show.
In any case, to get numeric results to compare correctly, numeric (not numeric-edited) variables should be used as the TS is going to do. |
|
Back to top |
|
|
|