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 a-var 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 ABS-A-VAR PIC S9(3)V9(2) COMP-3.
*---compute absolute value of A-VAR
IF A-VAR < 0
THEN
COMPUTE ABS-A-VAR = A-VAR * -1
ELSE
MOVE A-VAR TO ABS-A-VAR
END-IF.
COMPUTE ?????? = ?????? ABS-A-VAR ??????
|
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 ABS-OF-A PIC 9(3)V9(2) COMP-3.
After moving data from A to ABS-OF-A 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: 435 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 vice-versa)
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 A-VAR < 0
THEN
COMPUTE ABS-A-VAR = 0- A-VAR
ELSE
MOVE A-VAR TO ABS-A-VAR
END-IF. |
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 unsigned-fld to unsigned-fld
This strips the sign whether it's pos or neg. No compare is required. |
|
Back to top |
|
|
|