Post Reply 
(41) Round To The Nearest 1/n
04-13-2018, 06:27 PM
Post: #4
RE: (41) Round To The Nearest 1/n
(04-12-2018 08:24 PM)Mike (Stgt) Wrote:  I get the same this way:
Code:
 01*LBL "RNR"
 02 X<>Y
 03 ST+ X
 04 1/X
 05 +
 06 RCL X
 07 LASTX
 08 ST+ X
 09 MOD
 10 -
 11 .END.

Wow, that's a quite ...creative solution. I'm not sure if I understand completely how this works. For a more straightforward approach see below.

(04-12-2018 08:24 PM)Mike (Stgt) Wrote:  Note: Do not round "twice", rounding a result to a new n-th.
Example: round \(\pi\) to next 32th results \(\frac{101}{32}\), rounding this to next 16th results \(\frac{51}{16}\). In contrast, \(\pi\) to next 16th is \(\frac{50}{16}\), a difference of a full 16th.

Sure. You once round \(\pi\) and once \(\frac{101}{32}\). So the results are different.

(04-12-2018 08:24 PM)Mike (Stgt) Wrote:  Dieter's hint int(x+0,5) would take two bytes less. Wink

Right – 9 bytes without LBL and END.
But here x and n must have identical signs:

Code:
01 LBL "RNR"
02 STO Z
03 x
04 ,5
05 +
06 INT
07 X<>Y
08 /
09 END

But with 2 bytes more there is a way to handle also different signs:

Code:
01 LBL "RNR"
02 STO Z
03 x
04 ENTER
05 SIGN
06 2
07 /
08 +
09 INT
10 X<>Y
11 /
12 END

Finally, here is a TI-59 version.
Data entry is done "the HP way": x [x<>t] n. ;-)

Code:
000 LBL
001 A
002 x
003 x<>t
004 +
005 OP
006 10
007 /
008 2
009 =
010 INT
011 /
012 x<>t
013 =
014 RTN

Or with the already mentioned EE INV EE rounding method:

Code:
000 LBL
001 A
002 x
003 x<>t
004 =
005 FIX
006 0
007 EE
008 INV
009 EE
010 INV
011 FIX
012 /
013 x<>t
014 =
015 RTN

Of course real TI-59 programmers would avoid "=" and use parentheses instead. ;-)

Dieter
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: (41) Round To The Nearest 1/n - hth - 04-12-2018, 06:12 PM
RE: (41) Round To The Nearest 1/n - Dieter - 04-12-2018, 06:57 PM



User(s) browsing this thread: 1 Guest(s)