HP Forums
Rounding to the Nearest Reciprocal - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (/forum-10.html)
+--- Forum: HP Prime Software Library (/forum-15.html)
+--- Thread: Rounding to the Nearest Reciprocal (/thread-9578.html)



Rounding to the Nearest Reciprocal - Eddie W. Shore - 11-26-2017 09:10 PM

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).

Code:
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;

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


RE: Rounding to the Nearest Reciprocal - chromos - 11-28-2017 05:04 AM

This is exactly the case that fits into user defined functions (when they will remember the variables checkboxes).

PHP Code:
IP(A)+SIGN(A)*ROUND(FP(ABS(A))*N,0)/

[attachment=5363]

Otherwise, very nice utility, Eddie!


RE: Rounding to the Nearest Reciprocal - salvomic - 11-28-2017 08:30 AM

(11-28-2017 05:04 AM)chromos Wrote:  ...very nice utility, Eddie!

yes, thanks Eddie!

Salvo


RE: Rounding to the Nearest Reciprocal - Eddie W. Shore - 11-30-2017 01:55 PM

Thank you Chromos and Salvo!


RE: Rounding to the Nearest Reciprocal - Dieter - 12-01-2017 07:57 AM

(11-28-2017 05:04 AM)chromos Wrote:  
PHP Code:
IP(A)+SIGN(A)*ROUND(FP(ABS(A))*N,0)/

I do not own a Prime, so this may be a very dumb question, but is there a special reason why this can't be done with a simple ROUND(A*N,0)/N ?

Dieter