Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

Finding DIFFERENCE between two variables

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming
View previous topic :: :: View next topic  
Author Message
AJAYREDDY

New User


Joined: 17 Feb 2007
Posts: 47
Location: USA

PostPosted: Mon Jan 28, 2008 6:56 pm    Post subject: Finding DIFFERENCE between two variables
Reply with quote

Hi,

I have a situation where I got 2 variables A and B. I need to find the difference between 2 and display the correct results.

Example: If A = 11 and B = -4

I need to Display as 7

If A = -11 and B = -4

I need to Display as -7 (still I need difference between A and B, not -15)

If A = -11 and B = 4

I need to Display as -7

Is there any COMPUTE verb or any COBOL function where I can get the difference between 2 as above ? It should be able to do intelligently !!!

I do not know what will be the values inside A and B until it is calculated. But after calculation I should get results as above.

Please let me know for any ideas!!

Thanks
Back to top
View user's profile Send private message

Phrzby Phil

Active Member


Joined: 31 Oct 2006
Posts: 962
Location: Richmond, Virginia

PostPosted: Mon Jan 28, 2008 7:55 pm    Post subject:
Reply with quote

Difference means subtract. It gives you the distance between two numbers on the real number line.

If A=11 and B=-4, then the difference is not 7, and no correct code will give you that. A-B = 15, and B-A = -15.

It seems you want the difference of their absolute values. Either find an absolute value function, or do it yourself: if A < 0, then A = -A.
Back to top
View user's profile Send private message
enrico-sorichetti

Global Moderator


Joined: 14 Mar 2007
Posts: 10211
Location: italy

PostPosted: Mon Jan 28, 2008 8:12 pm    Post subject: Reply to: Finding DIFFERENCE between two variables
Reply with quote

the issue here is that the logic is confusing but

diff = sign(A) * ( abs(A) -abs(B) )

gives what You want for the values given,
what if abs(b) > abs(a)

a= 4 b=11 diff = ???
Back to top
View user's profile Send private message
AJAYREDDY

New User


Joined: 17 Feb 2007
Posts: 47
Location: USA

PostPosted: Mon Jan 28, 2008 8:27 pm    Post subject:
Reply with quote

Phrzby Phil wrote:
Difference means subtract. It gives you the distance between two numbers on the real number line.

If A=11 and B=-4, then the difference is not 7, and no correct code will give you that. A-B = 15, and B-A = -15.

It seems you want the difference of their absolute values. Either find an absolute value function, or do it yourself: if A < 0, then A = -A.


Hi Phil,

That is what I am looking for Absolute value function. If you know the syntax please let me know. I can do by programming logic as IF A<0 or B<0 etc. But I have about 20 to 25 fields to compare each other.

Thanks
Back to top
View user's profile Send private message
dick scherrer

Site Director


Joined: 23 Nov 2006
Posts: 19270
Location: Inside the Matrix

PostPosted: Mon Jan 28, 2008 10:35 pm    Post subject:
Reply with quote

Hello,

What should be the "difference" if A=4 and B=11?
Back to top
View user's profile Send private message
rag swain

New User


Joined: 17 Dec 2007
Posts: 33
Location: pune,INDIA

PostPosted: Mon Jan 28, 2008 10:49 pm    Post subject: Reply to: Finding DIFFERENCE between two variables
Reply with quote

You can use FUNCTION ABS with COMPUTE verb to get the absolute value of a variable. But then your requirement is not clear. If A < B, then how would you go for calculation?
Back to top
View user's profile Send private message
AJAYREDDY

New User


Joined: 17 Feb 2007
Posts: 47
Location: USA

PostPosted: Mon Jan 28, 2008 11:33 pm    Post subject: Re: Reply to: Finding DIFFERENCE between two variables
Reply with quote

rag swain wrote:
You can use FUNCTION ABS with COMPUTE verb to get the absolute value of a variable. But then your requirement is not clear. If A < B, then how would you go for calculation?


Yeah, that is where the main problem is. If A = 4 and B = 11 , I need value 7. It should not give me -7. If A=4 and B= -11 , it should still give me value 7 not ((4 - (-11))=15.

I can use the logic behind the scene as IF A<B ELSE A>B. But just curious to know if any COBOL function is there to get the results directly.

Thanks
Back to top
View user's profile Send private message
Phrzby Phil

Active Member


Joined: 31 Oct 2006
Posts: 962
Location: Richmond, Virginia

PostPosted: Tue Jan 29, 2008 12:02 am    Post subject:
Reply with quote

ABS(ABS(A) - ABS(B))
Back to top
View user's profile Send private message
Ganesh.Deokar

New User


Joined: 30 Sep 2005
Posts: 26
Location: Buffalo,NY

PostPosted: Tue Jan 29, 2008 12:16 am    Post subject:
Reply with quote

Try this. It's a simple one.

Define 3 more variables(WS-FIELD3, WS-FIELD4 and WS-FIELD5) without sign.

01 WS-TEMP.
05 WS-FIELD1 PIC -9(2).
05 WS-FIELD2 PIC -9(2).
05 WS-FIELD3 PIC 9(2).
05 WS-FIELD4 PIC 9(2).
05 WS-FIELD5 PIC 9(2).

1) Move your input values from WS-FIELD1 and WS-FIELD2 to WS-FIELD3 and WS-FIELD4 respectively.
2) Compute the difference between WS-FIELD3 and WS-FIELD4 in WS-FIELD5.
Back to top
View user's profile Send private message
the_gautam

Active User


Joined: 05 Jun 2005
Posts: 165
Location: Bangalore

PostPosted: Tue Jan 29, 2008 11:00 am    Post subject:
Reply with quote

IF A IS NEGATIVE
COMPUTE WS-A = 0 - A
ELSE
COMPUTE WS-A = A
END-IF.
IF B IS NEGATIVE
COMPUTE WS-B = 0 - B
ELSE
COMPUTE WS-B = B
END-IF.

COMPUTE WS-DIFFERENCE = A - B.

IF A IS NEGATIVE
COMPUTE WS-DIFFERENCE = 0 - WS-DIFFERENCE
END-IF.
Back to top
View user's profile Send private message
stodolas

Active Member


Joined: 13 Jun 2007
Posts: 632
Location: Wisconsin

PostPosted: Tue Jan 29, 2008 6:35 pm    Post subject:
Reply with quote

Phil gave the easiest and most elegant answer this other code is just reinventing the wheel and not really straight forward.
Back to top
View user's profile Send private message
enrico-sorichetti

Global Moderator


Joined: 14 Mar 2007
Posts: 10211
Location: italy

PostPosted: Tue Jan 29, 2008 6:49 pm    Post subject: Reply to: Finding DIFFERENCE between two variables
Reply with quote

It would be nice and rewarding for the responders if the O/P would care to
clarify his question

from the original question I guess that the O/P wants also the sign of the first operand

diff = sign(A) * abs( abs(A) - abs(B) )

... who knows ...

ok about the absolute value of the difference of the absolute values,
but why the sign of the first operand ???
Back to top
View user's profile Send private message
AJAYREDDY

New User


Joined: 17 Feb 2007
Posts: 47
Location: USA

PostPosted: Tue Feb 05, 2008 9:21 pm    Post subject: Reply to: Finding DIFFERENCE between two variables
Reply with quote

Thanks everyone. I did same as ABS function and manipulated the results which I need.
Back to top
View user's profile Send private message
dick scherrer

Site Director


Joined: 23 Nov 2006
Posts: 19270
Location: Inside the Matrix

PostPosted: Tue Feb 05, 2008 9:45 pm    Post subject: Reply to: Finding DIFFERENCE between two variables
Reply with quote

Cool icon_cool.gif

Thanks for letting us know it is working.

d
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts Difference between TWALENG and TWASIZE Arunkumar Chandrasekaran CICS 3 Tue Jan 03, 2017 12:57 pm
No new posts Protection Exception while move 0 to ... Kevin Vaz CICS 10 Tue Oct 18, 2016 4:19 pm
No new posts Sticky: difference between ... DUMMY ... and ... enrico-sorichetti JCL & VSAM 0 Mon Oct 17, 2016 4:31 pm
No new posts Finding unreferenced datasets without... Alan Playford JCL & VSAM 5 Tue Jul 12, 2016 9:07 pm
No new posts Is it possible to pass variables of s... Csongor CLIST & REXX 2 Fri Jun 10, 2016 7:33 pm


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us