(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 

04132018, 06:27 PM
Post: #4




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, 06:31 PM
(This post was last modified: 04142018 06:34 PM by Dieter.)
Post: #5




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, 09:03 PM
Post: #6




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: #7




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 

« Next Oldest  Next Newest »

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