Rounding x to the nearest 1/n.

53 Views Asked by At

I'm trying to come up with a way to solve this problem:

Assume x and y are rational numbers in the range (0, 1].
Given x, what is the nearest y to x such that yn=1 for some integer n?

Or put another way,

Round x to the nearest 1/n.

What I'm trying to do is find a good interval for a table I'm creating. I've computed the optimal interval, x, but I also want the table to land on every whole integer as well.

I'm trying to find the function that performs this transformation.

Examples:

f(x) = y f(0.3168705857) = 1/3 f(0.2141233120) = 1/5 f(0.9583990664) = 1 f(0.2426311964) = 1/4 f(0.8125500321) = 1 f(0.3569497599) = 1/3 f(0.0539263572) = 1/19

1

There are 1 best solutions below

0
On BEST ANSWER

$0 < x \le 1$ so

$1 \le \frac 1x$ so

$n \le \frac 1x \le n + 1$ for some integer $n \ge 1$. $n = \lfloor \frac 1x \rfloor$. $n + 1 = \lceil \frac 1x \rceil$. So

$\frac 1{\lceil 1/x \rceil} \le x \le \frac 1{\lfloor 1/x \rfloor}$.

Example: if $x = e^{-3} = 0.04978706836786394297934241565006$

Then $\frac 1x = 20.085536923187667740928529654582$ and

$\frac 1{21} < x < \frac 1{20}$

$\frac 1{20} - x =0.000212931632136057020657584349938$

and $x - \frac 1{21} = 0.00216802074881632393172336803101$

so closest is $\frac 1{20}$.