Are there any continuous “totally rational” functions besides piecewise first-order polynomial or rational functions? Let $I \subseteq \mathbb{R}$ be an interval and $f: I \to \mathbb{R}$ a continuous function. We’ll say that $f$ is totally rational if the following propositions are true for any $x\in I$:


*

*If $x \in \mathbb{Q}$ then $f(x) \in \mathbb{Q}$


*If $f(x) \in \mathbb{Q}$ then $x \in \mathbb{Q}$

A simple example of such a function is the identity function $f(x)=x$. More generally any function of the form $f(x)=ax + b$ with $a,b\in \mathbb{Q}$ will do. Another class of functions that are totally rational are those of the form
$$f(x)=\frac{ax + b}{cx + d}\qquad \text{with}\ a,b,c,d\in \mathbb{Q} \  \text{and}\ x\neq-\frac{d}{c}.$$
Besides functions of these kinds (and piecewise combinations thereof) I cannot find any other examples of such functions. It is easy to see, for instance, that any higher-order polynomial or rational function will fail condition (2).
But do other totally rational functions exist?

Here's a different type of example with the property:
$f(n + 0.a_1 a_2 a_3 \dots) = 0.0 a_1 0 a_2 0 a_3 \dots$
or
$f(n + \sum_{i \ge 1}{a_i \cdot 10^{-i}}) = \sum_{i \ge 1}{a_i \cdot 100^{-i}}$
In other words, the function takes the decimal expansion of the fractional part and inserts a $0$ between every digit.  Or it writes it in base $10$ and reads it again in base $100$.
The function maps numbers with eventually periodic expansions (rational numbers) to numbers with eventually periodic expansions, and the converse is also true.  Also it is continuous.
