The following warnings occurred:
Warning [2] count(): Parameter must be an array or an object that implements Countable - Line: 795 - File: showthread.php PHP 7.4.33 (FreeBSD)
File Line Function
/showthread.php 795 errorHandler->error





Post Reply 
Rounding to the Nearest Reciprocal
11-26-2017, 09:10 PM (This post was last modified: 11-28-2017 04:17 AM by Eddie W. Shore.)
Post: #1
Rounding to the Nearest Reciprocal
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
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


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



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