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-12-2018, 08:24 PM (This post was last modified: 04-13-2018 03:59 PM by Mike (Stgt).)
Post: #4
RE: (41) Round To The Nearest 1/n
(04-12-2018 01:10 PM)Eddie W. Shore Wrote:  Examples:

x = \(\pi\)
n = 10, result: 3.10 (nearest 10th)
n = 1000, result: 3.142 (nearest 1000th)
n = 16, result: 3.125 (nearest 16th)

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.

First enter N, next the number you'd like to process, then run RNR. If you prefere the entry sequence the other way round just leave out line 2.

Ciao.....Mike
=====
EDIT:

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.

EDIT 2:

Dieter's hint int(x+0,5) would take two bytes less. Wink

/M.
Find all posts by this user
Quote this message in a reply
04-13-2018, 06:27 PM
Post: #5
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, 08:18 AM
Post: #6
RE: (41) Round To The Nearest 1/n
(04-13-2018 06:27 PM)Dieter Wrote:  Wow, that's a quite ...creative solution. I'm not sure if I understand completely how this works.

That is what I did after I saw your int(x+0,5) suggestion. So the initial momentum was your hint. How it works? Instead of normalizing x to do the int(x+0,5) I "stay in the n-th world" and do there a x'-FRC(x') with x' = x + 1/(2n). For this the annoying RCL X. (By the way, my routine preserves one stack level.)

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

If you take the entry of n disregarding its sign, it is ok from my point of view.

BTW, you may round \(\pi\) to the nearest 113th to get the well known \(\frac{355}{113}\) approximation -- or the other one: \(\pi\), 7, XEQ "RNR", 7, * Smile
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: #7
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, 08:09 PM
Post: #8
RE: (41) Round To The Nearest 1/n
(04-14-2018 06:31 PM)Dieter Wrote:  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.

Irrespective of n's sign?

Quote:The approximation of \(-\pi\) is –3,125 and not +3,125. So the signs of input and output should match.

For positive n do doubt. Same for negative n? Sure? How did you prove that?

\(\pi\), 16, XEQ "RNR" -> 3,125 -- agreed
\(\pi\), -16 (negative!), XEQ "RNR" -> also 3,125 -- positive! even though n is negative. How come?

Ciao.....Mike
Find all posts by this user
Quote this message in a reply
04-14-2018, 09:03 PM
Post: #9
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.

X<> c
-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: #10
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
04-16-2018, 12:09 AM
Post: #11
RE: (41) Round To The Nearest 1/n
(04-14-2018 09:03 PM)Joe Horn Wrote:  IMHO that's the correct answer because, [...]

"IMHO" ony? Would you still doubt if there is an analytic proof?

Yes I know, sorry for that, my questions are not fair play, as -- meanwhile -- I am convinced the results' sign is correct. But first, I had to convince myself with a "proof", nothing big, only an algebraic transposition (see my answer to Dieter).

Ciao.....Mike
Find all posts by this user
Quote this message in a reply
04-16-2018, 01:39 AM (This post was last modified: 04-16-2018 04:06 PM by Mike (Stgt).)
Post: #12
RE: (41) Round To The Nearest 1/n
First, when I programmed this "short through" routine I had only positive arguments in mind. That it works correctly for negative arguments is pure coincidence.

(04-15-2018 04:13 PM)Dieter Wrote:  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.

Yes, but why?
Let me start with the "all positive" case, \(\pi\), 16, XEQ "RNR" -> 3,125. To see how many 1/16-th this are I have to divide the result by 1/16 (or multiply by 16) and get 50, so \(\displaystyle \frac{50}{16} \approx \pi\).
Now same again with n = -16. \(\pi\), -16, XEQ "RNR" -> 3.125. How many 1/(-16)-th are this? Exactly the same calculation as before: \(\displaystyle \frac{3.125}{\textstyle \frac{1}{\color{Red}{–16}}}={\displaystyle \color{Red}{–50}}\). As such \(\displaystyle \frac{\color{Red}{–50}}{\color{Red}{–16}}\approx \pi\)
This algebraic transposition convinced me. Smile

Quote:In simple words: z has to be an integer, but it doesn't have to be a natural number.
- Yes, 1, 2, 3, ... and the additive neutral 0, and the additive inverse –1, –2, –3, ...

Quote:In our example \(\pi\) rounded to the nearest –1/16 is 3,125, which is –49/–16. Or z=–49.
No, –50.

Quote:The essential point here: \(z\in\mathbb{Z}\) and not only \(z\in\mathbb{N}\).
"z has to be an integer" (your words), but neither x nor n. Example: 5.21, Pi, XEQ "RNR" -> 5.09296, this multiplied by Pi -> 16. So \(\displaystyle \frac{16}{\pi}\approx 5.21\) -- in case you need this. Wink

Ciao.....Mike

EDIT: replaced \(\sim\) by the more appropriate \(\approx \)
2nd edit: minor enhancement of formulas readability
Find all posts by this user
Quote this message in a reply
04-17-2018, 11:40 AM
Post: #13
RE: (41) Round To The Nearest 1/n
Thought this was quite useful - and so I added this to GEIR.ROM: https://github.com/isene/hp-41_GEIR.ROM
Find all posts by this user
Quote this message in a reply
04-17-2018, 02:17 PM
Post: #14
RE: (41) Round To The Nearest 1/n
(04-17-2018 11:40 AM)Geir Isene Wrote:  Thought this was quite useful - and so I added this to GEIR.ROM: ...

Consider, what you prefere the stack looks after RNR?
Code:
Before -- EXQ "RNR" -- After   --   or   

T: Reg_T      ->       Reg_Z        n
Z: Reg_Z      ->       Reg_Z        n
Y: x          ->       Reg_Z        n
X: n          ->       RNR(x,n)     RNR(x,n)
L: .dc.       ->       x-RNR(x,n)   n

Either one stack level is preserved \(\scriptstyle (Reg_Z)\) and LastX has an intermediate value -- or -- RNR(x) in Reg. X and the rest of the stack and LastX is filled with n. Your choice as the size of the routines is the same, assuming you picked Dieter's sign-aware RNR.
No! Not really a choice, as herewith I put my "quite ...creative solution" under the Q Public License version 1.0. To avoid for certain any drawbacks for your project, just do not use it. Wink

Ciao.....Mike
Find all posts by this user
Quote this message in a reply
Post Reply 




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