Rounding Revisited
#1

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.

#2

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
+ - -
\>>
#3

Followed by a second try ;-)

40.5 # 165Ch
\<<
DUP 2 MOD
100000000001
DUP2
+ -
+ -
\>>
#4

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.

#5

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?

#6

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".



Possibly Related Threads…
Thread Author Replies Views Last Post
  17BII & 17BII+ Discounted Payback Period Revisited Tom Neudorfl 8 2,805 11-25-2013, 10:28 AM
Last Post: Don Shepherd
  Touch periodic table on HP Prime - revisited Terje Vallestad 2 1,384 11-23-2013, 11:47 AM
Last Post: Mic
  HP Prime program: rounding to a fraction Patrice 3 1,660 10-31-2013, 06:16 AM
Last Post: Joe Horn
  HP Prime: Rounding error in determinant Stephan Matthys 3 1,607 10-25-2013, 09:29 PM
Last Post: Walter B
  Rounding of 10^x Olivier De Smet 8 2,498 08-28-2013, 06:33 AM
Last Post: Dieter
  A slide rule to conquer the moon revisited Palmer O. Hanson, Jr. 12 3,499 06-19-2013, 03:05 PM
Last Post: Ken Shaw
  HEPAX Revisited: Revision 1E available Ángel Martin 4 1,564 05-03-2013, 01:57 PM
Last Post: Diego Diaz
  Estimating Accumulated Rounding Errors Egan Ford 13 3,195 08-16-2012, 01:49 PM
Last Post: Egan Ford
  Calculators on youtube revisited:OT again, but is this a TI-92 Les Koller 2 1,326 07-26-2012, 05:45 AM
Last Post: Les Koller
  Classic (HP-67) charger issues revisited Matt Agajanian 6 2,264 05-20-2012, 10:50 PM
Last Post: Matt Agajanian

Forum Jump: