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

Why COMPUTE ROUNDED not giving exact value


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

New User


Joined: 28 Nov 2006
Posts: 69
Location: India

PostPosted: Mon Feb 26, 2007 1:34 pm
Reply with quote

Code:


01  WS-VAR-1              PIC 9(9).   
01  WS-VAR-2              PIC 9(9)V99.

COMPUTE WS-VAR-1 ROUNDED  = (365 / 12) * 11     => 334

COMPUTE WS-VAR-2 = (365 / 12) * 11
COMPUTE WS-VAR-1 ROUNDED = WS-VAR-2             => 335


Do i need to store result in some variable ??
Why COMPUTE WS-VAR-1 ROUNDED = (365 / 12) * 11 not giving 335 directly ?
Back to top
View user's profile Send private message
sachin_star3
Warnings : 1

New User


Joined: 30 Sep 2006
Posts: 78
Location: pune

PostPosted: Mon Feb 26, 2007 3:40 pm
Reply with quote

answer:

in compute with rouded always gives result approximate
but in 2nd case you have not done computation
i.e 34.58 = 35

so its better you
mention
ws-var-1 pic 9(9)v9
so you will get 34.6
correct me i am wronge
from -
sachin borase
pune
Back to top
View user's profile Send private message
William Thompson

Global Moderator


Joined: 18 Nov 2006
Posts: 3156
Location: Tucson AZ

PostPosted: Mon Feb 26, 2007 3:46 pm
Reply with quote

Intermediate results?
What would
COMPUTE WS-VAR-1 ROUNDED = (365.0 / 12.0) * 11.0
result in?
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Mon Feb 26, 2007 10:46 pm
Reply with quote

Hello,

Please look up ROUNDED in your Fine Manual. Rounded results are not approximate - they are exact to whatever you specify.

In your example, none of the fields have any decimal places defined, so only whole numbers are considered. If the "answer" was 334.9976, you would get 334. Rounding occurs with digits to the right of the decimal. Significant digits are not "rounded up". Let's say you have 1045 and divide it by 55 - the answer is 19. Should that round up to 20? I don't believe so. . . .
Back to top
View user's profile Send private message
ibm4me

New User


Joined: 23 Oct 2006
Posts: 3

PostPosted: Tue Feb 27, 2007 8:23 am
Reply with quote

picture clause

PIC 9(3)V9. ---->1
PIC 9(3). ----->2

Value
123.25 (1)
123 (2)

Truncated
123.2 (1)
123 (2)

Rounded
123.3 (1)
123 (2)


The ROUNDED option takes effect when, after decimal point alignment, the result calculated must be truncated on the right hand side.

The option adds 1 to the receiving item when the leftmost truncated digit has an absolute value of 5 or greater.




Correct me if am wrong
Back to top
View user's profile Send private message
hsk

New User


Joined: 28 Nov 2006
Posts: 69
Location: India

PostPosted: Tue Feb 27, 2007 11:41 am
Reply with quote

COMPUTE WS-VAR-1 ROUNDED = (365.0 / 12.0) * 11.0 is resulting in 334


if i replace the formula (365.0 / 12.0) * 11.0 by number 334.58 then the ans is 335

That is what confusion is ?
If it's rounding up 334.58 to 335,
then why it is not rounding when i use formula (365.0 / 12.0) * 11.0 and not number 334.58
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Wed Feb 28, 2007 5:01 am
Reply with quote

Hello,

If you use (365.00 / 12) * 11 it will give the rounding you expect.

Previously, there was not enough precision. This from the Fine Manual:
Quote:
Note: You must define the operands of any arithmetic statements with
enough decimal places to give the desired accuracy in the final result.
Back to top
View user's profile Send private message
William Thompson

Global Moderator


Joined: 18 Nov 2006
Posts: 3156
Location: Tucson AZ

PostPosted: Wed Feb 28, 2007 5:10 am
Reply with quote

Well, I was close.... icon_redface.gif
Back to top
View user's profile Send private message
hsk

New User


Joined: 28 Nov 2006
Posts: 69
Location: India

PostPosted: Wed Feb 28, 2007 9:46 am
Reply with quote

oohh, thanks dick .........

that works ..........
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Wed Feb 28, 2007 8:21 pm
Reply with quote

You're welcome icon_smile.gif
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 CSQBGET - Call giving completion code... COBOL Programming 3
No new posts Compute statement with Cobol Z/os 5.2 COBOL Programming 2
No new posts WRITE to VSAM giving duplicate RC CICS 1
No new posts DB2 Program abending without giving a... DB2 7
No new posts Join giving more records than expected SYNCSORT 2
Search our Forums:

Back to Top