Rounding a real number w.r.t. a given amount of steps

85 Views Asked by At

Let $x$ be a real number, $x \in [0,1]$. Suppose a system can only provide a noisy signal about the value of $x$, given the granularity allowed by the system, $N \in \mathbb{N}^*$.

I'm looking for an equation/formula to round up $x$ given the number of steps $N$ existing within $[0,1]$. See example below.

Example

$x = 0.3$

$N = 2$, meaning that within the range [0,1] the system can only take either the value of $0$ or $1$. Given this granularity, the system should "round up" $x$ to $0$.

$N=5$, the system can now take the values $0$, $\frac{1}{4}$, $\frac{1}{2}$, $\frac{3}{4}$, $1$. Given this new granularity, $x$ should become $\frac{1}{4}$.

EDIT: Preferably not using floor or ceiling functions, i.e.

$$\frac {\lfloor x \cdot N \rfloor}{N -1}$$

2

There are 2 best solutions below

5
On BEST ANSWER

You can use the following function for calculating floor:

$$f(x)=x-\frac{1}{2}-\frac{\arcsin(\sin(\pi(x-\frac{1}{2})))}{\pi}$$


Then, for your purpose, you can simply use:

$$\frac{f(xN)}{N-1}$$


If it helps, then you can also use the following function for calculating ceiling:

$$f(x)=x+\frac{1}{2}+\frac{\arctan(\tan(\pi(-x-\frac{1}{2})))}{\pi}$$


If memory serves correctly, these functions yield incorrect results for negative integers.

Your question, however, implies that you are interested only in positive real numbers...

2
On

$$\frac{\lfloor x\cdot N \rfloor}{N-1}$$ should fit your needs...but if you don't like floor use $\lfloor x \rfloor = x - \bmod(x,1)$ instead...