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

Rounding off error


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

New User


Joined: 08 Jan 2006
Posts: 22
Location: Delhi

PostPosted: Sat Sep 06, 2008 6:34 am
Reply with quote

Hi,

Look at this code:
WS-VAR1 pic 9(2)V9(9) value 1.939491221.
WS-VAR2 pic 9(2)V9(3) value 0.

Multiply 1 by WS-VAR1 giving WS-VAR2 ROUNDED.

After executing the above code, i was expecting 1.940 in WS-VAR2 but strangely it had 1.939. It seems to me that since WS-VAR2 has only 3 decimal places so it was rounding off the resultant value from 4th decimal places.

What should I do so that it does the rounding off from the last decimal place of the resultant value not just 1 decimal place more than the receiver field?

Thanks,
Sumit
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: Sat Sep 06, 2008 7:06 am
Reply with quote

Hello,

Quote:
i was expecting 1.940 in WS-VAR2 but strangely it had 1.939
I believe you need to change your expectation. In your example, ROUND is working exactly as it should, not strangely.
Quote:
When the size of the fractional result exceeds the number of places provided for its storage, truncation occurs unless ROUNDED is specified. When ROUNDED is specified, the least significant digit of the resultant identifier is increased by 1 whenever the most significant digit of the excess is greater than or equal to 5.
The most significant digit of the excess is 4, the result is not "rounded up".
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: Sat Sep 06, 2008 7:14 am
Reply with quote

Hello again,

Code:
WS-VAR1 pic 9(2)V9(9) value 1.939491221.
WS-VAR2 pic 9(2)V9(3) value 0.

You might try:
Code:

COMPUTE WS-VAR2 = WS-VAR1 + .000999999
I'm not able to test just now due to some system maintenance, but i believe that will give the result you want.

If not, post back here and i'll figure out where i went wrong icon_smile.gif
Back to top
View user's profile Send private message
futuredba

New User


Joined: 08 Jan 2006
Posts: 22
Location: Delhi

PostPosted: Sat Sep 06, 2008 5:52 pm
Reply with quote

Hi,

According to the defination of rounding off in math, that i was taught during my school days, 1.93949 should be first rounded up to 1.9395 and it should be again rounded up to 1.940. So i thought cobol keyword ROUNDED is not doing what rounding means in mathematics.

But I just searched some web sites over internet and found that rounding off in mathematics means exactly what ROUNDED doing in cobol.

I didn't want to make the number 1.940 intentionally. I just wanted to make sure that it is rounded off correctly as far as mathematics is corcerned.

Thanks for you reply,
Sumit
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


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

PostPosted: Sat Sep 06, 2008 7:09 pm
Reply with quote

Sumit, I've never heard of rounding one digit at a time as you were taught in school -- I've always heard rounding only applies to the digit after the last significant digit and 0 to 4 round down, 5 to 9 round up. I guess even after getting a bachelor's degree in mathematics, there's still stuff to learn!
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: Sun Sep 07, 2008 12:00 am
Reply with quote

Hello Sumit,

Quote:
Thanks for you reply,
You're welcome.

Thank you for the reply telling us that all is well now icon_smile.gif

d
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 Error to read log with rexx CLIST & REXX 11
No new posts Error when install DB2 DB2 2
No new posts CLIST - Virtual storage allocation error CLIST & REXX 5
No new posts Error while running web tool kit REXX... CLIST & REXX 5
No new posts Getting Error while trying to establi... DB2 3
Search our Forums:

Back to Top