Thursday, April 12, 2018

HP Prime, HP 41C, TI-58C: Round to the Nearest 1/n


HP Prime, HP 41C, TI-58C:  Round to the Nearest 1/n

Introduction

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.

HP Prime Program RNEAR

EXPORT RNEAR(x,n)
BEGIN
// round x to the nearest nth
// 2018-04-07 EWS
RETURN ROUND(FP(x)*n,0)/n+IP(x);
END;

HP 41C and Swiss Micros SM41 Program RNEAR

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

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

TI-58C/TI-59 Program RNEAR

Store x into register 01
Store n into register 02
The result is stored in register 03
Minimum Registers Needed: 4 (I always leave register 00 for general use)

There is no rounding function on the TI-58C and TI-59, hence the rounding routine needs to be manually entered.

Step
Key Code
Key
Notes
000
43
RCL

001
01
01

002
22
INV

003
59
Int
INV Int: Frac
004
65
*

005
43
RCL

006
02
02

007
95
=

008
42
STO

009
03
03

010
00
0

011
93
.

012
05
5

013
32
x<>t
Store 0.5 to t
014
43
RCL

015
03
03

016
22
INV

017
59
Int

018
77
x≥t

019
55
[÷]
If frac(R03)≥0.5, go to label “÷”
020
76
Lbl

021
65
[*]
Label “×”
022
43
RCL

023
03
03

024
59
Int

025
55
÷

026
43
RCL

027
02
02

028
85
+

029
43
RCL

030
01
01

031
59
Int

032
95
=

033
42
STO

034
03
03

035
91
R/S
Display result
036
81
RST
End program
037
76
Lbl

038
55
[÷]
Label “÷”
039
01
1

040
44
SUM

041
03
03

042
61
GTO

043
65
[*]


Examples:

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

Eddie

All original content copyright, © 2011-2018.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.  Please contact the author if you have questions.