Smooth floor function

2.3k Views Asked by At

I want a monotonic function on the positive real numbers that behaves like floor but in smooth way, like smoothstep but for all integers. It should follow this simple rule.

slope is zero at integers and slope is maximum at median of two adjacent integers or in other words: slope is at $0$ when fract$(x) = 0$ and slope is at maximum when fract$(x)=0.5$

So far i was able to approximate this function my self with help of trigonometric functions. $$ f(x) = \lfloor x \rfloor + \max\left(\frac{(-1)^{\lfloor x\rfloor}}{2}(1-\cos(\pi x)), \frac{(-1)^{\lfloor x+1\rfloor}}{2}(1-\cos(\pi x + \pi))\right) $$ can this be achieved in a simpler/shorter way?

2

There are 2 best solutions below

4
On BEST ANSWER

What do yo think of : $$y(x)=x-\frac{\sin(2\pi x)}{2\pi}$$

enter image description here

0
On

The floor function is equivalent to x-sawtooth(x), where sawtooth is the sawtooth wave function. By taking a trigonometric approximation of the sawtooth wave from https://mathematica.stackexchange.com/questions/38293/make-a-differentiable-smooth-sawtooth-waveform we can derive an approximation of floor:

δ = 0.01;
trg[x_] := 1 - 2 ArcCos[(1 - δ) Sin[2 π x]]/π;
sqr[x_] := 2 ArcTan[Sin[2 π x]/δ]/π;
swt[x_] := (1 + trg[(2 x - 1)/4] sqr[x/2])/2;
flr[x_] := x-swt[x]

where δ is a parameter that controls the approximation. Here is a picture of the resulting function: https://i.stack.imgur.com/VH6zT.png