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
 

 

Compute statement not working

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

New User


Joined: 14 Mar 2012
Posts: 69
Location: India

PostPosted: Wed Apr 02, 2014 7:12 pm    Post subject: Compute statement not working
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: 7913
Location: Bellevue, IA

PostPosted: Wed Apr 02, 2014 7:26 pm    Post subject:
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: 69
Location: India

PostPosted: Wed Apr 02, 2014 7:38 pm    Post subject:
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

DFSORT Moderator


Joined: 09 Mar 2011
Posts: 7232

PostPosted: Wed Apr 02, 2014 7:57 pm    Post subject: Reply to: Compute statement not working
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: 1238
Location: Richfield, MN, USA

PostPosted: Wed Apr 02, 2014 11:15 pm    Post subject:
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: 69
Location: India

PostPosted: Thu Apr 03, 2014 11:07 am    Post subject:
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    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 -913/-911 Deadlock during UPDATE stat... NoSleep319 DB2 5 Fri Nov 18, 2016 12:37 am
No new posts COBOL DB2 - CALL statement - high CPU... TS70363 DB2 15 Sun Sep 11, 2016 6:07 am
No new posts SCRNAME not working on my mainframe? mrsidhu12 TSO/ISPF 7 Thu Sep 01, 2016 2:17 pm
No new posts Converting NULL column into NOT NULL ... Raghu navaikulam DB2 5 Sat Aug 06, 2016 3:45 pm
This topic is locked: you cannot edit posts or make replies. Updating value of key using rewrite s... ParminderKumar COBOL Programming 4 Thu Aug 04, 2016 12:46 am


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