View previous topic :: View next topic
|
Author |
Message |
futuredba
New User
Joined: 08 Jan 2006 Posts: 22 Location: Delhi
|
|
|
|
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 |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
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 |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
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 |
|
Back to top |
|
|
futuredba
New User
Joined: 08 Jan 2006 Posts: 22 Location: Delhi
|
|
|
|
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 |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8696 Location: Dubuque, Iowa, USA
|
|
|
|
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 |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
Hello Sumit,
Quote: |
Thanks for you reply, |
You're welcome.
Thank you for the reply telling us that all is well now
d |
|
Back to top |
|
|
|