The Museum of HP Calculators

HP Forum Archive 19

[ Return to Index | Top of Index ]

Rounding Revisited
Message #1 Posted by Rodger Rosenbaum on 16 July 2009, 2:06 a.m.

There was a mini-challenge involving rounding back in 2003:

MC: round up/down

I said at the time:

"It should be noted that the reason some of these various algorithms are failing with the test values of Meyers and Prange is due to a property of the floating point system on the HP48. There is a largest odd number that can be represented on the 48 and that is 999999999999. Numbers greater than this are all even integers; their fractional parts are zero. Adding one to a number greater than 999999999999 on the 48 will not change it from even to odd (these remarks don't apply to internal 15 form numbers; their threshold is higher). Any algorithm that expects this property (change of parity with addition of unity) to hold for all integers may fail."

There are probably some on this forum who didn't see or participate in that topic. It might be interesting to look at it again.

      
Re: Rounding Revisited
Message #2 Posted by Werner on 18 July 2009, 1:36 p.m.,
in response to message #1 by Rodger Rosenbaum

Following the link, apparently no correct program that handled any input was ever produced?

here's my first try, then ;-)

 @ Round to Odd
 @ 50.5 bytes 
 @ # 7FB5h
 \<<
   DUP 2 MOD
   SWAP OVER - SWAP
   500000000001 DUP ROT
   + - -
 \>>
      
Re: Rounding Revisited
Message #3 Posted by Werner on 19 July 2009, 5:21 a.m.,
in response to message #1 by Rodger Rosenbaum

Followed by a second try ;-)

40.5 # 165Ch
\<<
 DUP 2 MOD
 100000000001
 DUP2
 + -
 + -
\>>
            
Re: Rounding Revisited
Message #4 Posted by Rodger Rosenbaum on 21 July 2009, 11:57 p.m.,
in response to message #3 by Werner

Here's one I came up with that works (I think) on all positive integers, including near the EOPR (defined above):

35.0 bytes

Round to odd integer

<< DUP 2 MOD 3 2 / == - 0 RND >>

It doesn't work for negative integers.

It seems to me that you can find very short routines that work for all positive and negative numbers, but that fail near the EOPR. Or, you can find short routines that work for only positive (or negative) numbers, and don't fail near the EOPR. But you can't (so far) find short routines that work for both positive and negative numbers, and don't fail near the EOPR.

                  
Re: Rounding Revisited
Message #5 Posted by Werner on 22 July 2009, 2:49 a.m.,
in response to message #4 by Rodger Rosenbaum

40.5 bytes for a routine that works for any input vs. 35 for one that works for positive inputs only is not so bad, is it?

                        
Re: Rounding Revisited
Message #6 Posted by Rodger Rosenbaum on 22 July 2009, 2:29 p.m.,
in response to message #5 by Werner

No, it's not bad at all. In reality I suppose if I really needed to do this function, I'd just take one of the examples from that long ago thread and add a test to take care of the exceptions. But, that's not as much fun.

I was just commenting that it's annoying that the EOPR causes such trouble. One of my professors used to say that the "perversity of the universe is maximized".


[ Return to Index | Top of Index ]

Go back to the main exhibit hall