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

How to round off till most significant byte


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

New User


Joined: 03 Nov 2006
Posts: 87
Location: Plymouth, MN USA

PostPosted: Sat Jan 12, 2008 6:54 pm
Reply with quote

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
View user's profile Send private message
Craq Giegerich

Senior Member


Joined: 19 May 2007
Posts: 1512
Location: Virginia, USA

PostPosted: Sat Jan 12, 2008 10:17 pm
Reply with quote

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
View user's profile Send private message
mmwife

Super Moderator


Joined: 30 May 2003
Posts: 1592

PostPosted: Sun Jan 13, 2008 11:03 pm
Reply with quote

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
View user's profile Send private message
Craq Giegerich

Senior Member


Joined: 19 May 2007
Posts: 1512
Location: Virginia, USA

PostPosted: Mon Jan 14, 2008 12:28 am
Reply with quote

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
View user's profile Send private message
Amit Banerjee

New User


Joined: 12 Jan 2008
Posts: 14
Location: india

PostPosted: Tue Jan 15, 2008 1:44 am
Reply with quote

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
View user's profile Send private message
Craq Giegerich

Senior Member


Joined: 19 May 2007
Posts: 1512
Location: Virginia, USA

PostPosted: Tue Jan 15, 2008 1:48 am
Reply with quote

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
View user's profile Send private message
stodolas

Active Member


Joined: 13 Jun 2007
Posts: 631
Location: Wisconsin

PostPosted: Tue Jan 15, 2008 2:02 am
Reply with quote

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
View user's profile Send private message
Amit Banerjee

New User


Joined: 12 Jan 2008
Posts: 14
Location: india

PostPosted: Tue Jan 15, 2008 2:09 am
Reply with quote

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
View user's profile Send private message
stodolas

Active Member


Joined: 13 Jun 2007
Posts: 631
Location: Wisconsin

PostPosted: Tue Jan 15, 2008 2:14 am
Reply with quote

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
View user's profile Send private message
stodolas

Active Member


Joined: 13 Jun 2007
Posts: 631
Location: Wisconsin

PostPosted: Tue Jan 15, 2008 2:25 am
Reply with quote

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
View user's profile Send private message
Phrzby Phil

Senior Member


Joined: 31 Oct 2006
Posts: 1049
Location: Richmond, Virginia

PostPosted: Tue Jan 15, 2008 6:52 am
Reply with quote

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
View user's profile Send private message
stodolas

Active Member


Joined: 13 Jun 2007
Posts: 631
Location: Wisconsin

PostPosted: Tue Jan 15, 2008 7:17 am
Reply with quote

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 icon_sad.gif Now I just read math books for entertainment.
Back to top
View user's profile Send private message
Phrzby Phil

Senior Member


Joined: 31 Oct 2006
Posts: 1049
Location: Richmond, Virginia

PostPosted: Tue Jan 15, 2008 9:59 pm
Reply with quote

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
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 Data set Rec-Cnt and Byte-Cnt Testing & Performance 2
No new posts 10 byte RBA conversion DB2 2
No new posts 10 byte RBA conversion -non applicati... JCL & VSAM 1
No new posts Dataset size increase on adding 1 byt... DFSORT/ICETOOL 8
No new posts Updating a 1 byte thats in occurs mul... DFSORT/ICETOOL 6
Search our Forums:

Back to Top