View previous topic :: View next topic
|
Author |
Message |
sandeep kumar302
New User
Joined: 14 Mar 2012 Posts: 81 Location: India
|
|
|
|
Hi,
In my cobol program the compute is not working properly. Please guide me on where i am making mistakes. please find the sample of my source below-
Code: |
01 WS-INIT-VARS.
05 COST-CHG-REC PIC 9(3) VALUE ZEROS COMP-3.
01 WS-USED-VARS.
05 COST-CHG-COUNT PIC 9(9) VALUE ZEROS COMP-3.
05 MCF-COUNT PIC 9(9) VALUE ZEROS COMP-3.
0000-MAIN-PARA.
*------------------*
INITIALIZE WS-INIT-VARS
ACCEPT CCHECK-PARMIN FROM SYSIN
MOVE CCHECK-PARMIN TO WS-CCHECK-PARMIN.
DISPLAY 'WS-LOWER-LIMIT:' WS-CCHK-LOWER-VALUE
DISPLAY 'WS-UPPER-LIMIT:' WS-CCHK-UPPER-VALUE
MOVE 60 TO COST-CHG-COUNT
DISPLAY 'COST-CHG-COUNT: ' COST-CHG-COUNT
MOVE 1000 TO MCF-COUNT
DISPLAY 'MCF-COUNT: ' MCF-COUNT
COMPUTE COST-CHG-REC ROUNDED
= (COST-CHG-COUNT / MCF-COUNT) * 100
ON SIZE ERROR
MOVE 010 TO COST-CHG-REC
DISPLAY 'COST-CHG-REC: ' COST-CHG-REC
EVALUATE TRUE
WHEN COST-CHG-REC < WS-CCHK-LOWER-VALUE
DISPLAY 'COST CHANGE RECS ' COST-CHG-REC '%' ' < '
WS-CCHK-LOWER-VALUE '%' ' LOWER-LIMIT'
DISPLAY ' '
DISPLAY 'MOVING 1 TO RC'
MOVE 1 TO RETURN-CODE
WHEN COST-CHG-REC > WS-CCHK-UPPER-VALUE
DISPLAY 'COST CHANGE RECS ' COST-CHG-REC '%' ' > '
WS-CCHK-UPPER-VALUE '%' ' UPPER-LIMIT'
DISPLAY ' '
DISPLAY 'MOVING 1 TO RC'
MOVE 1 TO RETURN-CODE
WHEN OTHER
DISPLAY 'COST CHANGE RECS= ' COST-CHG-REC '%'
DISPLAY ' '
END-EVALUATE.
STOP RUN.
0000-MAIN-PARA-EXIT.
----------------------*
EXIT.
|
I am getting the below output-
WS-LOWER-LIMIT:07
WS-UPPER-LIMIT:14
COST-CHG-COUNT: 000000060
MCF-COUNT: 000001000
COST-CHG-REC: 000
COST CHANGE RECS 000% < 07% LOWER-LIMIT
MOVING 1 TO RC
Expected output is:
-------------------------
WS-LOWER-LIMIT:07
WS-UPPER-LIMIT:14
COST-CHG-COUNT: 000000060
MCF-COUNT: 000001000
COST-CHG-REC: 006
COST CHANGE RECS 006% < 07% LOWER-LIMIT
MOVING 1 TO RC |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8697 Location: Dubuque, Iowa, USA
|
|
|
|
Doing the division first means your intermediate result is going to be zero. Try coding
Code: |
COMPUTE COST-CHG-REC ROUNDED
= 100 * COST-CHG-COUNT / MCF-COUNT |
|
|
Back to top |
|
|
sandeep kumar302
New User
Joined: 14 Mar 2012 Posts: 81 Location: India
|
|
|
|
Oh Great. That worked. Thanks a lot for your help Robert.
Is there any way where even if i do division, the intermediate resul is not zero. Just curious to know, if any.
And yes again, many thanks for the help above. |
|
Back to top |
|
|
Bill Woodger
Moderator Emeritus
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
|
|
|
|
In a COMPUTE you must always multiply first, and divide last. Otherwise you lose significance.
Go through the calculation, as though you were the computer:
60 / 1000 stored with 3 integer places and one decimal place (due to specifying ROUNDED). Gets you zero.
Always do multiplies first, divides last. Use ( and ) to ensure that a human reader knows the order you have intended everything, rather than what the compiler is going to do by following its rules.
Where is the output from your first DISPLAY? Did you check your compile listing for messages? Everytime you have a statement with a conditional part (ON SIZE ERROR is conditional) you should end it with the appropriate scope-terminator, in this case END-COMPUTE.
Using ON SIZE ERROR is a bit tacky anyway. Just make the field big enough to hold any result possible, and test the size afterwards. |
|
Back to top |
|
|
Terry Heinze
JCL Moderator
Joined: 14 Jul 2008 Posts: 1249 Location: Richfield, MN, USA
|
|
|
|
For a detailed explanation of precison of intermediate results, see Appendix A of the COBOL Programming Guide. |
|
Back to top |
|
|
sandeep kumar302
New User
Joined: 14 Mar 2012 Posts: 81 Location: India
|
|
|
|
Thanks Bill for the detailed explanation and thanks Terry for the sugestion. |
|
Back to top |
|
|
|