Portal | Manuals | References | Downloads | Info | Programs | JCLs | Mainframe wiki | Quick Ref
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Profile Log in to check your private messages Log in
 
How to round off till most significant byte

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming
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    Post subject: How to round off till most significant byte
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    Post subject: Re: How to roundi till most significant byte
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    Post subject:
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    Post subject:
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    Post subject:
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    Post subject:
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: 632
Location: Wisconsin

PostPosted: Tue Jan 15, 2008 2:02 am    Post subject:
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    Post subject:
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: 632
Location: Wisconsin

PostPosted: Tue Jan 15, 2008 2:14 am    Post subject:
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: http://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: 632
Location: Wisconsin

PostPosted: Tue Jan 15, 2008 2:25 am    Post subject:
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.
http://en.wikipedia.org/wiki/Rounding#Round-to-even_method[/u]
Back to top
View user's profile Send private message
Phrzby Phil

Active Member


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

PostPosted: Tue Jan 15, 2008 6:52 am    Post subject:
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: 632
Location: Wisconsin

PostPosted: Tue Jan 15, 2008 7:17 am    Post subject:
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

Active Member


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

PostPosted: Tue Jan 15, 2008 9:59 pm    Post subject:
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    IBMMAINFRAMES.com Support Forums -> COBOL Programming All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts Copy 4 byte of data from the last rec... arunsoods DFSORT/ICETOOL 9 Fri Oct 06, 2017 12:15 pm
No new posts Extract Data till prev hour balaji81_k DB2 16 Fri Aug 25, 2017 9:25 pm
No new posts 2 byte YEAR from Y'DATE2' field tecnokrat DFSORT/ICETOOL 2 Tue Feb 07, 2017 12:05 pm
No new posts Need Help to Define 36 Input Fields (... satish.ms10 CLIST & REXX 2 Sat May 07, 2016 3:08 pm
No new posts Copy key from previous record till it... kranthikumarb DFSORT/ICETOOL 6 Wed Aug 26, 2015 7:47 pm

Facebook
Back to Top
 
Job Vacancies | Forum Rules | Bookmarks | Subscriptions | FAQ | Polls | Contact Us