Rounding to the
Nearest Reciprocal (HP Prime, TI-84
Plus CE, Casio fx-CG 50)
Introduction
The program ROUNDRCP rounds a number to the nearest 1/n.
This function can come in handy in several applications, for example, when
working with construction or measuring, when have to round results to the
nearest eighth (1/8), inch (1/12) or sixteenth (1/16).
Presented here are versions for the HP Prime, TI-84 Plus CE,
and Casio fx-CG 50.
HP Prime: ROUNDRCP
EXPORT ROUNDRCP(a,n)
BEGIN
// Round a to the
nearest 1/n
// 2017-11-21 EWS
LOCAL w;
w:=FP(ABS(a))*n;
w:=ROUND(w,0);
RETURN
IP(a)+SIGN(a)*(w/n);
END;
The TI-84 Plus CE and Casio fx-CG 50 versions store the
answer in X.
TI-84 Plus CE ROUNDRCP
"EWS
2017-11-21"
Input
"NUMBER:",A
Input "TO
1/NTH:",N
fPart(abs(A))*N→W
round(W,0)→W
If A<0
Then
iPart(A)-(W/N)→X
Else
iPart(A)+(W/N)→X
End
Disp
"X:",X
Casio fx-CG 50 ROUNDRCP
“NUMBER”?→A
“TO 1÷NTH”?→N
Frac ((Abs A))*N→N
RndFix(W,0)→W
If A<0
Then
Int (A)-(W÷N)→X
Else
Int(A)+(W÷N)→X
IfEnd
“X:”
X
Examples
Round π to the nearest 1/4:
ROUNDRCP(π, 4): 3.25
= 13/4
Round e^2 to the nearest 1/100:
ROUNDRCP(e^2, 100): 7.39 = 739/100
Round 1/4 + 2/7 + 3 1/3 to the nearest 1/16:
ROUNDRCP(1/4 + 2/7 + 3 + 1/3, 16): 3.875 = 31/8
(thank you to Joe Horn for pointing out my error, it's correct now.)
(thank you to Joe Horn for pointing out my error, it's correct now.)
Tips
If you do not have the sign (signum) function, you can compensate
by the following code:
If X<0
Then
Return -1 (subtract operation)
Else
Return 1 (addition
operation)
End
If your calculator does not have the Round to any number of
decimal points function, such as the Casio fx-5800p or fx-3650p, you can manipulate
the modes and use the Rnd function and a switch of modes, like this code:
Fix 0
X (put number to round in display)
Rnd
Float/Norm (1/2)
Eddie
This blog is property of Edward Shore, 2017