View previous topic :: View next topic
|
Author |
Message |
star_dhruv2000
New User
Joined: 03 Nov 2006 Posts: 87 Location: Plymouth, MN USA
|
|
|
|
Hi all,
I have following data definitions
Code: |
05 VAR1 V9(17).
05 VAR2 V9(02).
MOVE .12444444444444447 TO VAR1.
COMPUTE VAR2 ROUNDED = VAR1.
DISPLAY VAR2. |
The output I am expecting is .13 but I am getting .12 in VAR2. I Investigated and found that only last 4 is getting rounded and rest figure remains same i.e. 1244444444444445. This is why I am not getting proper data in output.
Is there any way I can round these digits.
Thanks in advance.. |
|
Back to top |
|
|
Craq Giegerich
Senior Member
Joined: 19 May 2007 Posts: 1512 Location: Virginia, USA
|
|
|
|
star_dhruv2000 wrote: |
Hi all,
I have following data definitions
Code: |
05 VAR1 V9(17).
05 VAR2 V9(02).
MOVE .12444444444444447 TO VAR1.
COMPUTE VAR2 ROUNDED = VAR1.
DISPLAY VAR2. |
The output I am expecting is .13 but I am getting .12 in VAR2. I Investigated and found that only last 4 is getting rounded and rest figure remains same i.e. 1244444444444445. This is why I am not getting proper data in output.
Is there any way I can round these digits.
Thanks in advance.. |
According to the rules for arithmatic rounding you got the results you should have. |
|
Back to top |
|
|
mmwife
Super Moderator
Joined: 30 May 2003 Posts: 1592
|
|
|
|
Hi Star,
Quote: |
4 is getting rounded and rest figure remains same i.e. 1244444444444445. |
This should give you the hint you need to figure it out. |
|
Back to top |
|
|
Craq Giegerich
Senior Member
Joined: 19 May 2007 Posts: 1512 Location: Virginia, USA
|
|
|
|
When rounding to 2 decimal places only the third decimal place is tested to see if the second decimal place needs to be changed. Find an elementary school math book and look it up. |
|
Back to top |
|
|
Amit Banerjee
New User
Joined: 12 Jan 2008 Posts: 14 Location: india
|
|
|
|
hi Dhruv,
the rule with 5 is that if the next digit is <5 then it's rounded to the same value but if it's greater than 5 it's rounded to the next higher value.
i.e if u round 0.225 to 2 places u'll get 0.22
but for 0.275 u'll get 0.28. |
|
Back to top |
|
|
Craq Giegerich
Senior Member
Joined: 19 May 2007 Posts: 1512 Location: Virginia, USA
|
|
|
|
Amit Banerjee wrote: |
hi Dhruv,
the rule with 5 is that if the next digit is <5 then it's rounded to the same value but if it's greater than 5 it's rounded to the next higher value.
i.e if u round 0.225 to 2 places u'll get 0.22
but for 0.275 u'll get 0.28. |
Wrong, your example doesn't agree with what you are saying. |
|
Back to top |
|
|
stodolas
Active Member
Joined: 13 Jun 2007 Posts: 631 Location: Wisconsin
|
|
|
|
Rounding is done by first truncating one position past the place you want to round to then applying rounding rules.
.222222222222222225 rounded to 2 places is evaluated like this:
.222
with a final result of .22
.2225
.222
with a final result of .22
.225999999999999
.225
with a final result of .23
This is how it works when done by hand or a computer. |
|
Back to top |
|
|
Amit Banerjee
New User
Joined: 12 Jan 2008 Posts: 14 Location: india
|
|
|
|
in the 1st case since 2<5 it's rounded off to 2 only and the result is 0.22.
in the 2nd case 7>5 and thus gets rounded off to 8 resulting to 0.28.
That's what's happenning in dhruv's case also
initial 0.1244444444444447
since last digit is 7,4 is rounded off to 5
thus 0.124444444444445
now since last digit is 5 and 4<5,it gets rounded to 4 giving
0.12444444444444
now it remains 4 till the end and the final number is 0.12 |
|
Back to top |
|
|
stodolas
Active Member
Joined: 13 Jun 2007 Posts: 631 Location: Wisconsin
|
|
|
|
Amit - you are wrong. Whether or not a position in the number is rounded has nothing to do with the value of that position, only the digit to the right positionally has an affect on rounding.
The OP is wants to see "chain rounding" which is not how rounding works. Only the digit to the direct right of the position to be rounded affects its value.
"Round 436.449 to the nearest whole number.
Chain rounding would round 436.449 to 436.45 and then to 436.5 and then to 437. That is not correct. Instead, round only based on the number closest to the number you want to round to. In this case, the first 4 after the decimal point is the key; therefore, the answer is 436."
See the rules of rounding here: www.dtc.dla.mil/qa/RR.htm |
|
Back to top |
|
|
stodolas
Active Member
Joined: 13 Jun 2007 Posts: 631 Location: Wisconsin
|
|
|
|
From the COBOL manual - that the OP should have consulted.
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 in the OP's example is 4 and therefor does not cause the value to round to .13. If he wants to do chain rounding (which is completely wrong mathematically) then he needs to define vars for each size all the way down to the result he wants
Code: |
05 VAR1 V9(17).
05 VARa V9(16).
05 VARb V9(15).
05 VARc V9(14).
05 VARd V9(13).
05 VARe V9(12).
...
05 VARl V9(05).
05 VARm V9(04).
05 VARn V9(03).
05 VAR2 V9(02).
MOVE .12444444444444447 TO VAR1.
COMPUTE VARA ROUNDED = VAR1.
COMPUTE VARB ROUNDED = VARA.
COMPUTE VARC ROUNDED = VARB.
...
COMPUTE VARN ROUNDED VARM.
COMPUTE VAR2 ROUNDED = VARM.
DISPLAY VAR2.
|
Also see this Wikipedia article on rounding.
en.wikipedia.org/wiki/Rounding#Round-to-even_method[/u] |
|
Back to top |
|
|
Phrzby Phil
Senior Member
Joined: 31 Oct 2006 Posts: 1050 Location: Richmond, Virginia
|
|
|
|
Wow. This stuff should be elementary.
You can string as many digits as you want to the right in you examples, but as Steve and others have said, the determining digit is the one immediately to the right of the position being rounded to.
My credentials: adjunct mathematics instructor at a community college, where I teach a remedial math class. |
|
Back to top |
|
|
stodolas
Active Member
Joined: 13 Jun 2007 Posts: 631 Location: Wisconsin
|
|
|
|
1 Advanced Math class shy of a math minor. I was away from the calculus for 4 years and didn't use it. All the classes I needed required working knowledge of calc Now I just read math books for entertainment. |
|
Back to top |
|
|
Phrzby Phil
Senior Member
Joined: 31 Oct 2006 Posts: 1050 Location: Richmond, Virginia
|
|
|
|
Steve -
I heartily recommend the classic 4-vol set The World of Mathematics, ed. by James R. Newman. 2500 pages from ancient to modern (as of 1956, but who's counting (pun intended)?). Original sources. |
|
Back to top |
|
|
|