View previous topic :: :: View next topic

Author 
Message 
fundu
New User
Joined: 05 Oct 2005 Posts: 2




Hi,
How to change the sign of a packed decimal.
i have a variable like
05 avar pic s9(3)v99 defined in a copybook
Suppose user entered negative value, I want to change the sign to positive for specific calculations. How can i do that, how can i check that the variable contains Positive value.
Thanks in advance,
Vijay KS 

Back to top 




DavidatK
Active Member
Joined: 22 Nov 2005 Posts: 700 Location: Troy, Michigan USA




Vijay KS,
I think the old fashion way is the most readable and simple.
Code: 
05 ABSAVAR PIC S9(3)V9(2) COMP3.
*compute absolute value of AVAR
IF AVAR < 0
THEN
COMPUTE ABSAVAR = AVAR * 1
ELSE
MOVE AVAR TO ABSAVAR
ENDIF.
COMPUTE ?????? = ?????? ABSAVAR ??????

There are other ways, and I?m sure some will chime in with others.
Dave 

Back to top 


cheryala
New User
Joined: 20 Mar 2006 Posts: 46




Hi,
I think one more working storage variable
05 ABSOFA PIC 9(3)V9(2) COMP3.
After moving data from A to ABSOFA use it in the expressions..
Let me know if i'm wrong..
Cheers
RSC 

Back to top 


radhakrishnan82
Active User
Joined: 31 Mar 2005 Posts: 436 Location: chennai, India




I agree that there are other ways to do the same.
I do agree that mulitipying a ve number with 1 can result a positive value of the same number .Thinking about performance perspective,mulitiplication is a result of repeated addition.So whenever we perform multiplication, it performs addition process,internally.
While holding huge data,this may be a important factor considering its performance.(converting +ve to ve or viceversa)
Instead of multiplying a ve number with 1 to convert it
as a postive value, its better to subtract the ve number with 0.
Code: 
IF AVAR < 0
THEN
COMPUTE ABSAVAR = 0 AVAR
ELSE
MOVE AVAR TO ABSAVAR
ENDIF. 
Its an optimisation technique.
Hope it helps. 

Back to top 


mmwife
Super Moderator
Joined: 30 May 2003 Posts: 1592




If you will always need a positive value in the field and it comes to you as part of a record, the most efficient way to assure it's positive is to redefine it as PIC 9.. (without the S), then move it to itself. E.g.:
move unsignedfld to unsignedfld
This strips the sign whether it's pos or neg. No compare is required. 

Back to top 


