IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Comparison of numeric edited variables


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
Nileshkul

New User


Joined: 09 May 2016
Posts: 43
Location: India

PostPosted: Sat Mar 16, 2019 1:18 am
Reply with quote

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
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8700
Location: Dubuque, Iowa, USA

PostPosted: Sat Mar 16, 2019 3:11 am
Reply with quote

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
View user's profile Send private message
sergeyken

Senior Member


Joined: 29 Apr 2008
Posts: 2145
Location: USA

PostPosted: Sat Mar 16, 2019 10:53 pm
Reply with quote

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
View user's profile Send private message
Nileshkul

New User


Joined: 09 May 2016
Posts: 43
Location: India

PostPosted: Sat Mar 16, 2019 11:09 pm
Reply with quote

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
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8700
Location: Dubuque, Iowa, USA

PostPosted: Sat Mar 16, 2019 11:33 pm
Reply with quote

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
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts Repeat n times by substuting SEQNUM i... DFSORT/ICETOOL 2
No new posts Preserve changes to ISPF Panel Variables TSO/ISPF 5
No new posts Issues Converting From ZD to Signed N... DFSORT/ICETOOL 4
No new posts Convert HEX to Numeric DB2 3
No new posts Find a record count/numeric is multip... COBOL Programming 1
Search our Forums:

Back to Top