Post Reply 
(41) Round To The Nearest 1/n
04-12-2018, 01:10 PM
Post: #1
(41) Round To The Nearest 1/n
Original Blog Entry: https://edspi31415.blogspot.com/2018/04/...arest.html

The program RNEAR rounds a number x to the nearest 1/n. For example, to round x to the nearest 10th, n = 10. To round to the nearest 16th, n = 16.

Keystrokes: x [ENTER] y [XEQ] [ALPHA] RNEAR [ALPHA]
Code:

01 LBL^T REAR
02 FIX 0
03 STO 01
04 X<>Y
05 STO 02
06 RFC
07 *
08 RND
09 RCL 01
10 /
11 RCL 02
12 INT
13 +
14 FIX 4
15 RTN


Examples:

x = π
n = 10, result: 3.10 (nearest 10th)
n = 1000, result: 3.142 (nearest 1000th)
n = 16, result: 3.125 (nearest 16th)
Visit this user's website Find all posts by this user
Quote this message in a reply
04-12-2018, 06:12 PM (This post was last modified: 04-12-2018 07:04 PM by hth.)
Post: #2
RE: (41) Round To The Nearest 1/n
What is the magic instruction RFC? I am not familiar with it.

Håkan
Find all posts by this user
Quote this message in a reply
04-12-2018, 06:57 PM (This post was last modified: 04-12-2018 07:12 PM by Dieter.)
Post: #3
RE: (41) Round To The Nearest 1/n
(04-12-2018 06:12 PM)hth Wrote:  What is the magic instruction RFC? I do not familiar with it.

I think that's supposed to be a "FRC".

But, Eddie: why do you split x into integer and fractional part? Why should this be required?

Hint re. your blog post: int(x+0,5) rounds to the nearest integer. This even works on a TI-59. ;-)
But there is also a way to round a number to display precision: EE INV EE does it.

Dieter
Find all posts by this user
Quote this message in a reply
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
04-14-2018, 06:31 PM (This post was last modified: 04-14-2018 06:34 PM by Dieter.)
Post: #5
RE: (41) Round To The Nearest 1/n
(04-14-2018 08:18 AM)Mike (Stgt) Wrote:  About the sign, I am not sure if this is correct (BTW, I discarded the X<>Y previously in line 2):
\(\pi\), 16, XEQ "RNR" -> 3,125
\(-\pi\), 16, XEQ "RNR" -> -3,125
\(\pi\), -16, XEQ "RNR" -> 3,125
\(-\pi\), -16, XEQ "RNR" -> -3,125

I'd say this is exactly the way it's supposed to be. If you enter \(-\pi\) the output of course also has to be negative.
The approximation of \(-\pi\) is –3,125 and not +3,125. So the signs of input and output should match.

Dieter
Find all posts by this user
Quote this message in a reply
04-14-2018, 09:03 PM
Post: #6
RE: (41) Round To The Nearest 1/n
(04-14-2018 08:09 PM)Mike (Stgt) Wrote:  \(\pi\), 16, XEQ "RNR" -> 3,125 -- agreed
\(\pi\), -16 (negative!), XEQ "RNR" -> also 3,125 -- positive! even though n is negative. How come?

IMHO that's the correct answer because, of all the infinitely many multiples of 1/(-16), the one nearest to pi is in fact 3.125 ... and rounding x to the nearest multiple of 1/n is the program's stated purpose.

<0|ɸ|0>
-Joe-
Visit this user's website Find all posts by this user
Quote this message in a reply
04-15-2018, 04:13 PM (This post was last modified: 04-15-2018 04:15 PM by Dieter.)
Post: #7
RE: (41) Round To The Nearest 1/n
(04-14-2018 08:09 PM)Mike (Stgt) Wrote:  For positive n do doubt. Same for negative n? Sure? How did you prove that?

Simple. We are looking for a value of z for which z/n is as close to x as possible. The program then returns z/n. If n<0 then z has to have the opposite sign of x.

In simple words: z has to be an integer, but it doesn't have to be a natural number.
In our example \(\pi\) rounded to the nearest –1/16 is 3,125, which is –49/–16. Or z=–49.
The essential point here: \(z\in\mathbb{Z}\) and not only \(z\in\mathbb{N}\).

On the other hand –3,125 cannot be the correct answer as it isn't the closest –1/16 at all. Even 0 is closer to \(\pi\) than –3,125. ;-)

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




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