(41) Round To The Nearest 1/n

04122018, 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:
Examples: x = π n = 10, result: 3.10 (nearest 10th) n = 1000, result: 3.142 (nearest 1000th) n = 16, result: 3.125 (nearest 16th) 

04122018, 06:12 PM
(This post was last modified: 04122018 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 

04122018, 06:57 PM
(This post was last modified: 04122018 07:12 PM by Dieter.)
Post: #3




RE: (41) Round To The Nearest 1/n
(04122018 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 TI59. ;) But there is also a way to round a number to display precision: EE INV EE does it. Dieter 

04122018, 08:24 PM
(This post was last modified: 04132018 03:59 PM by Mike (Stgt).)
Post: #4




RE: (41) Round To The Nearest 1/n
(04122018 01:10 PM)Eddie W. Shore Wrote: Examples: I get the same this way: Code: 01*LBL "RNR" 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 nth. 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. /M. 

04132018, 06:27 PM
Post: #5




RE: (41) Round To The Nearest 1/n
(04122018 08:24 PM)Mike (Stgt) Wrote: I get the same this way: 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. (04122018 08:24 PM)Mike (Stgt) Wrote: Note: Do not round "twice", rounding a result to a new nth. Sure. You once round \(\pi\) and once \(\frac{101}{32}\). So the results are different. (04122018 08:24 PM)Mike (Stgt) Wrote: Dieter's hint int(x+0,5) would take two bytes less. Right – 9 bytes without LBL and END. But here x and n must have identical signs: Code: 01 LBL "RNR" But with 2 bytes more there is a way to handle also different signs: Code: 01 LBL "RNR" Finally, here is a TI59 version. Data entry is done "the HP way": x [x<>t] n. ;) Code: 000 LBL Or with the already mentioned EE INV EE rounding method: Code: 000 LBL Of course real TI59 programmers would avoid "=" and use parentheses instead. ;) Dieter 

04142018, 08:18 AM
Post: #6




RE: (41) Round To The Nearest 1/n
(04132018 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 nth 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, * 

04142018, 06:31 PM
(This post was last modified: 04142018 06:34 PM by Dieter.)
Post: #7




RE: (41) Round To The Nearest 1/n
(04142018 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): 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 

04142018, 08:09 PM
Post: #8




RE: (41) Round To The Nearest 1/n
(04142018 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 

04142018, 09:03 PM
Post: #9




RE: (41) Round To The Nearest 1/n
(04142018 08:09 PM)Mike (Stgt) Wrote: \(\pi\), 16, XEQ "RNR" > 3,125  agreed 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 

04152018, 04:13 PM
(This post was last modified: 04152018 04:15 PM by Dieter.)
Post: #10




RE: (41) Round To The Nearest 1/n
(04142018 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 

04162018, 12:09 AM
Post: #11




RE: (41) Round To The Nearest 1/n
(04142018 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 

04162018, 01:39 AM
(This post was last modified: 04162018 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.
(04152018 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/16th 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. 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. Ciao.....Mike EDIT: replaced \(\sim\) by the more appropriate \(\approx \) 2nd edit: minor enhancement of formulas readability 

04172018, 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/hp41_GEIR.ROM


04172018, 02:17 PM
Post: #14




RE: (41) Round To The Nearest 1/n
(04172018 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 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 signaware 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. Ciao.....Mike 

« Next Oldest  Next Newest »

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