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

Rounding Logic.


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
divate.dilip

New User


Joined: 11 May 2009
Posts: 15
Location: Mumbai

PostPosted: Mon Aug 03, 2009 4:06 pm
Reply with quote

My input field is having value 120.
Now I am doing the calulation using the input value.

120/7.8=15.384615, but now when I multiply the 15.38 by 7.8 then 15.38*7.8=119.964 I am not getting the same value 120.

Is there any way of rounding, from which i can get the same value as 120 instead of 119.96
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


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

PostPosted: Mon Aug 03, 2009 4:52 pm
Reply with quote

There is a link to the manuals at the top of the page. Find the COBOL Language Reference manual and look up the ROUNDED option.
Back to top
View user's profile Send private message
Marso

REXX Moderator


Joined: 13 Mar 2006
Posts: 1353
Location: Israel

PostPosted: Mon Aug 03, 2009 6:04 pm
Reply with quote

If you would do 15.3846*7.8 then you would get 120 (rounded).
You need to increase the precision of your intermediary result, that's all.
(and use ROUNDED too).
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: Mon Aug 03, 2009 11:52 pm
Reply with quote

The way COBOL handles intermediate results is not what most people expect. Read carefully about them in that section of the Language Reference Manual.
Back to top
View user's profile Send private message
Marso

REXX Moderator


Joined: 13 Mar 2006
Posts: 1353
Location: Israel

PostPosted: Tue Aug 04, 2009 7:23 pm
Reply with quote

So, how do you do this anyway?

Do you use one or more COMPUTE statement(s) or DIVIDE and MULTIPLY?
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: Tue Aug 04, 2009 7:47 pm
Reply with quote

I don't have access to a mainframe at present, but if I remember correctly, the following should work:
Code:
01.
    05  WS-120         PIC S999      VALUE +120  COMP-3.
    05  WS-7POINT8     PIC S99V9     VALUE +7.8  COMP-3.
    05  WS-1ST-RESULT  PIC S999V9(4)             COMP-3.
    05  WS-2ND-RESULT  PIC S999                  COMP-3.

    DIVIDE   WS-120        BY WS-7POINT8 GIVING WS-1ST-RESULT ROUNDED
    MULTIPLY WS-1ST-RESULT BY WS-7POINT8 GIVING WS-2ND-RESULT ROUNDED
    .
WS-2ND-RESULT should equal WS-120 at this point.
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 Finding faulty logic Subscript out of... COBOL Programming 5
This topic is locked: you cannot edit posts or make replies. Need assistance in job scheduling logic. Mainframe Interview Questions 2
No new posts Rexx Logic error while adding seperat... CLIST & REXX 3
No new posts PL/1 Callback address logic in z/OS C... PL/I & Assembler 1
No new posts Sync logic between VSAM files and DB2... COBOL Programming 9
Search our Forums:

Back to Top