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

Compute statement not working


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

New User


Joined: 14 Mar 2012
Posts: 81
Location: India

PostPosted: Wed Apr 02, 2014 7:12 pm
Reply with quote

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

Global Moderator


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

PostPosted: Wed Apr 02, 2014 7:26 pm
Reply with quote

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

New User


Joined: 14 Mar 2012
Posts: 81
Location: India

PostPosted: Wed Apr 02, 2014 7:38 pm
Reply with quote

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

Moderator Emeritus


Joined: 09 Mar 2011
Posts: 7310
Location: Inside the Matrix

PostPosted: Wed Apr 02, 2014 7:57 pm
Reply with quote

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

JCL Moderator


Joined: 14 Jul 2008
Posts: 1249
Location: Richfield, MN, USA

PostPosted: Wed Apr 02, 2014 11:15 pm
Reply with quote

For a detailed explanation of precison of intermediate results, see Appendix A of the COBOL Programming Guide.
Back to top
View user's profile Send private message
sandeep kumar302

New User


Joined: 14 Mar 2012
Posts: 81
Location: India

PostPosted: Thu Apr 03, 2014 11:07 am
Reply with quote

Thanks Bill for the detailed explanation and thanks Terry for the sugestion.
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 SYNCSORT/ICETOOL JOINKEYS SORT Statem... DFSORT/ICETOOL 3
No new posts OMVS Shell Script not working properly. All Other Mainframe Topics 1
No new posts DFDSS COPY using Filter REFDT statement JCL & VSAM 2
No new posts SKIP LOCKED DATA in UPDATE statement DB2 9
This topic is locked: you cannot edit posts or make replies. Splice JCL into one record for DD sta... SYNCSORT 2
Search our Forums:

Back to Top