# Constructing a 2-periodic extension of the absolute value function using floor and ceiling functions

I am trying to use floor and ceiling functions to construct a 2-periodic extension of the function $$f(x) = |x|, -1 \leq x \leq 1$$.

Through trial an error I have been able to show that:

$$f(x) = 1 - \bigg( \lfloor x \rfloor - 2 \lfloor \frac{\lfloor x \rfloor}{2} \rfloor)(x - \lfloor x \rfloor) + (\lfloor x-1 \rfloor - 2\lfloor{\frac{\lfloor x-1 \rfloor}{2}}\rfloor)(\lceil x-1 \rceil -(x-1)\bigg)$$

However, this formula does not work when $$x$$ is an even integer since it gives 1 instead of 0.

Is there an easier way to do this?

• The $f(x)$ in the question seems different from the one in the accepted answer. Aug 7, 2020 at 20:47

If you can also use trigonometric functions, then there are several ways to do it.

If we can construct a graph like the following, which I will call $$r(x)$$ then $$f(x)=r(x)+r(-x)$$ will almost be what you want depending whether or not $$f(2k)=1$$ and $$f(2k+1)=0$$ for all integers $$k$$. First, let us create a periodic "cutting" function

$$c(x)=\lfloor1+\sin\pi x\rfloor$$ Now look at the graph of

$$g(x)=\lceil x\rceil-x$$ If we multiply $$g(x)$$ and $$c(x)$$ we will get a function whose graph looks like the first graph above. So we define

$$\begin{eqnarray} r(x)&=&g(x)c(x)\\ &=&(\lceil x\rceil-x)\lfloor1+\sin\pi x\rfloor \end{eqnarray}$$

And let

$$\begin{eqnarray} f_0(x)&=&r(x)+r(-x)\\ &=&(\lceil x\rceil-x)\lfloor1+\sin\pi x\rfloor+(\lceil -x\rceil+x)\lfloor1-\sin\pi x\rfloor \end{eqnarray}$$

This function has the following graph This is almost what we want, but we need to correct the missing points by adding a function which equals one for all even integers and equals zero elsewhere. A function such as

$$c(x)=\left\lfloor\frac{1+\cos\pi x}{2}\right\rfloor$$

So we define

$$f(x)=(\lceil x\rceil-x)\lfloor1+\sin\pi x\rfloor+(\lceil -x\rceil+x)\lfloor1-\sin\pi x\rfloor+\left\lfloor\frac{1+\cos\pi x}{2}\right\rfloor$$