Why is this combination of nearest-integer functions --- surprisingly --- continuous? Alright, I didn't know the best way to formulate my question. Basically, whilst doing some physics research, I naturally came upon the function
$$ f(x) = 2x[x] - [x]^2 $$
where I use $[x]$ as notation for the `nearest-integer function' (i.e. rounding off). Usually this function has to have a caveat of how we exactly define the value for $x \in \frac{1}{2} \mathbb Z$, but interestingly for this function it does not matter, since it turns out to be continuous! In fact, it turns out $f(x)$ is exactly given by the glued function of taking all the tangent lines of $x^2$ at integer values of $x$:

(Note: due to properties of $x^2$, the tangent lines exactly intersect at half-integer values of $x$.)
So my question is not literally `why is it continuous?', but rather: considering it is continuous, and considering that that is not a generic property of functions which are defined in terms of nearest-integer functions, is there a better (i.e. more insightful) way of expressing $f(x)$? Relatedly, is there some part of mathematics where functions similar to these naturally arise?
 A: 
is there a better (i.e. more insightful) way of expressing $f(x)$?

Maybe one way you could do that is by noticing that
$$
f(x) = \max_{k\in\mathbb{Z}} \left( 2kx-k^2 \right).
$$
Since, in any interval of fixed length, $f$ is the max of a finite number of continuous functions, then it is continuous.

is there some part of mathematics where functions similar to these naturally arise?

Personally, I have encountered this a lot in information theory, when many lower/upper bounds on communication rates are derived together, implying that the max/min of these bounds holds.
This is especially true when many different linear inequalities can be naturally derived and make intuitive sense.
For instance,
$$
\begin{cases}
R_1 \ge 2-4R_2\\
R_1 \ge 1-R_2
\end{cases}
\implies R_1 \ge \max\left\{ 2-4R_2, 1-R_2 \right\}.
$$
If you really want an example, off the top of my head I can think of Theorem 2 in this paper.
A: Let $g(x,y)$ be any continuous function such that $g(x,-\frac12)=g(x,\frac12)$. Then $f(x)=g(x,x-[x])$ is continuous.
In particular, your function is given by $g(x,y)=x^2-y^2$. Consequently, we can write $f(x)=x^2 - (x-[x])^2$.
A: The difference between the rounding function (piecewise constant) and $x$ is a triangle wave, i.e. piecewise linear, periodic, ranging in $[-\frac12,\frac12)$.
If you square it, you get a continuous, piecewise quadratic function. This is enough to explain your observation.

A: 
Given a $k+1$-times differentiable function $g$ such that for every integer $n$, $$\int_n^{n+1} g^{(k+1)}(t)(n+0.5-t)\,dt=0$$ we have that the following function is continous $$f(x) = g([x]) + \frac{(x-[x])^1}{1!}g'([x])+\frac{(x-[x])^2}{2!}g''([x])+\cdots+\frac{(x-[x])^k}{k!}g^{(k)}([x])$$
(In your case, $g(x) = x^2$ and $k=1$)

Observe that the function you've defined is equal to a bunch of tangent lines (or linear approximations) "stuck together". If you look at the graph, you'll see that at integer values, $f(x)$ is tangent to $x^2$. You can verify that this characterisation of your function $f$ is true because the function is locally linear, and equal to $x^2$ at integer values.
This leads to the question of: For which functions $g$ is it true that the linear approximation centered at $n\in \mathbb Z$ is equal to the linear approximation centered at $n+1$, at $x=n+0.5$. We will determine precisely when this is the case by using Taylor's theorem with the integral form of the remainder term.
So at $a=n$ and $x=n+0.5$, we have that the first order Taylor approximation is: $$g(n+0.5) =g(n) + 0.5g'(n) + \int_n^{n+0.5}g''(t)(n+0.5-t)\,dt$$
And at $a=n+1$ and $x=n+0.5$, we have that: $$g(n+0.5) = g(n+1) - 0.5g'(n+1) + \int_{n+1}^{n+0.5} g''(t)(n+0.5-t)\,dt$$
The two linear approximations centered at $a=n$ and $a=n+1$ agree at $x=n+0.5$ precisely when their remainder terms are the same, i.e.: $$\int_n^{n+0.5}g''(t)(n+0.5-t)\,dt =\int_{n+1}^{n+0.5} g''(t)(n+0.5-t)\,dt$$ which we can rearrange to $$\int_n^{n+1} g''(t)(n+0.5-t)\,dt=0$$ In particular, this is true whenever $g''(t)$ is symmetric around $n+0.5$.
From $g$, we can define a function $f$ which is equal to the tangent lines of $g$ stuck together, as follows: $$f(x) = (x - [x]) g'([x]) + g([x]) $$
A: Call a function $f : \mathbb{R} \rightarrow \mathbb{R}$ weird-continuous iff for all $x \in \mathbb{R}$, the left and right limits of $f$ at $x$ exist, whether or not they're equal. Define the weird derivative $\Delta f$ of a function $f : \mathbb{R} \rightarrow \mathbb{R}$ to be the function $\Delta f : \mathbb{R} \rightarrow \mathbb{R}$ defined by: $$\Delta(f)(x) = (\lim_+f)(x)-(\lim_-f)(x),$$ where for example the $(\lim_+f)(x)$ denotes the limit of $f(x')$ as a $x'$ approaches $x$ from the right.
Also, for each $a \in \mathbb{R}$, write $\langle a \rangle$ for the function $\mathbb{R} \rightarrow \mathbb{R}$ defined as follows:
$$\langle a \rangle (x) = \begin{cases} 1 & x = a \\ 0 & x \neq a\end{cases}$$
For example:

*

*if $H$ is the Heaviside step function, then $\Delta (H) = \langle 0\rangle$.

*if $H$ is the Heaviside step function, then $\Delta (x \mapsto 3H(x-1)+4H(x-2)) = 3\langle 1\rangle+4\langle 2\rangle$.

*if $f$ is continuous, then $\Delta(f) = 0$.

Letting $f$ and $g$ denote weird-continuous functions, the basic results (I think) are:

Proposition 0. Suppose $f : \mathbb{R} \rightarrow \mathbb{R}$ has no removable discontinuities. Then $f$ is continuous iff $\Delta f = 0$.
Proposition 1. Additivity $\Delta(f+g) = \Delta(f)+\Delta(g)$ and $\Delta(0) = 0$.
Proposition 2. Product Rule. $\Delta (fg) = \Delta(f)g + f \Delta(g)$
(Is the product rule even true? I don't really use it below...)
Corollary to the product rule. Weird derivatives are linear with respect to continuous functions, meaning that if $f$ is continuous, then $\Delta(fg) = f\Delta(g)$.

Now think of $x$ as the identity function $\mathbb{R} \rightarrow \mathbb{R}$. Let $f = 2 x [x] - [x]^2$.
To prove that $f$ is continuous, we'll show that $\Delta(f) = 0$. We have:
$$\Delta (f) = \Delta(2 x [x] - [x]^2) = 2 x \Delta(x) - \Delta([x]^2)$$
Also: $$\Delta([x]) = \left(\sum_{n \in \mathbb{Z}+\frac{1}{2}}\langle n\rangle\right)$$
Furthermore: $$\Delta([x]^2) = \sum_{n \in \mathbb{Z}+\frac{1}{2}}((n+1/2)^2 - (n-1/2)^2)\langle n\rangle = \sum_{n \in \mathbb{Z}+\frac{1}{2}}(n+1/2+n-1/2)(n+1/2-n+1/2)\langle n\rangle = \sum_{n \in \mathbb{Z}+\frac{1}{2}}2n\langle n\rangle$$
Hence: $$\Delta (f) = 2x\left(\sum_{n \in \mathbb{Z}+\frac{1}{2}}\langle n\rangle\right)-\sum_{n \in \mathbb{Z}+\frac{1}{2}}2n\langle n\rangle = \sum_{n \in \mathbb{Z}+\frac{1}{2}}2n\langle n\rangle-\sum_{n \in \mathbb{Z}+\frac{1}{2}}2n\langle n\rangle = 0$$
So $f$ is continuous.
A: Particularly I don't like rounding functions and I think $f$ could be expressed in a more appropriate form for derivation and integration.
So an alternative way to express $f$ would be as a series of functions as follows:
Let $\mathcal{X}_A$ the indicator function of the set $A$, $I_n = [ n - \frac{1}{2}, n + \frac{1}{2} [$ and $f_n(x) = (2nx-n^2) \mathcal{X}_{I_n}(x)$. Thus we can write $f$ as
$$ f(x) = \sum_{n \in \mathbb{Z}} f_n(x) $$
Which is immediately absolutely convergent, but more importantly, uniformly convergent on bounded sets. This means that derivation commutes with the summation on open bounded sets of $\mathbb{R} \setminus (\mathbb{Z} + \frac{1}{2})$ and that integration commutes with the summation on compact sets of $\mathbb{R}$.
However I don't have knowledge about where similar functions naturally arises in mathematics.
