TITLE: Functions that take rationals to rationals
QUESTION [47 upvotes]: What is known about $\mathcal C^\infty$ functions $\mathbb R\to\mathbb R$ that always take rationals to rationals? Are they all quotients of polynomials? If not, are there any that are bounded yet don't tend to a limit for $x\to +\infty$? If there are, then can we also require them to be analytic?
(This is basically just a random musing after I found myself using trigonometric functions twice in unrelated throwaway counterexamples. It struck me that it was kind of conceptual overkill to whip out a transcendental function for the purpose I needed: basically that it had to keep wiggling forever. I couldn't think of a nice non-trancendental function to do this job, however, and now wonder whether that is because they can't exist).

REPLY [16 votes]: The references given by Dave Renfro and Cocopuffs did not completely answer my question, but gave enough inspration that I think I've got it now. After several false starts:
Theorem. There exists an analytic function $\xi:\mathbb R\to[0,1]$ such that

$\xi(\mathbb Q)\subseteq \mathbb Q$.
$\xi(x)$ does not tend to a limit for $x\to\infty$.

Proof. Let $q_1, q_2, \ldots$ be a fixed enumeration of the rationals. Define by simultaneous induction a sequence of functions $f_0, f_1, f_2\ldots$ and integers $m_0<m_1<m_2\cdots$, as follows:
As (more or less arbitrary) base cases, let $f_0(x)=0$ and $m_0=1$.
For $n\ge 1$, let
$$h_n(x) = \prod_{i=1}^n(q_n-x) = a_{n0}+a_{n1}x+\cdots+a_{nn}x^n$$
(where the middle part defines the coefficients $a_{ni}$ on the right hand side) and
$$ f_n(x) = \frac{h_n(x)}{2^n\Bigr\lceil |a_{n0}|+|a_{n1}|m_{n-1}+\cdots+|a_{nn}|m_{n-1}^n \Bigr\rceil}$$
Note that $f_n$ is a polynomial with rational coefficients and $|f_n(z)|\le 2^{-n}$ for every complex $z$ with $|z|\le m_{n-1}$.
Now if $n$ is even let $m_n$ be first integer larger than $m_{n-1}$ such that $\sum_{i=0}^{n}f_i(m_n)\ge 2$. If $n$ is odd, let $m_n$ be the first integer larger than $m_{n-1}$ such that $\sum_{i=0}^{n}f_i(m_n)\le-2$. Since the leading coefficient of $h_n$ is $(-1)^n$ these conditions are always true for large enough $m_n$.
After defining all $f_n$, let
$$ g(x) = \sum_{i=0}^\infty f_n(x) $$
Properties of $g$:

The sum converges uniformly in the open ball $B_{m_n}(0)$ for all $n$, because except for the first $n$ terms, every $f_i$ is bounded absolutely by $2^{-i}$, and the sum of these converges.
Because uniform convergence in an open subset of $\mathbb C$ preserves analyticity, $g$ is analytic on every $B_{m_n}(0)$ and so on all of $\mathbb C$.
$g$ maps rationals to rationals. For any fixed $q$ there are finitely many terms in the sum for $g(q)$ before every $f_n(q)$ is zero by constructions.
$g(m_1), g(m_2), \ldots$ are alternately less than $-1$ and greater than $1$. By construction $\sum_{i=0}^{n} f_i(m_n)$ is less than $-2$ or greater than $2$, and the remaining terms in $g(m_n)$ cannot change the sum by more than $1$.
By the intermediate value theorem there is a zero of $g$ between every $m_n$ and $m_{n+1}$.

Finally, let
$$ \xi(x) = \frac{g(x)^2}{g(x)^2+1}$$
Then $\xi$ is analytic because $g$ is. Its range is trivially at most $[0,1]$. It maps rationals to rationals because $g$ does.
Also, $\xi$ cannot have a limit for $x\to\infty$. It is zero infinitely often, but it is also at least $\frac12$ at every $m_n$.
Remarks. The definition of $\xi$ is completely constructive, and $\xi(q)$ can be computed in finitely many steps for any $q\in\mathbb Q$.
Incidentally, $\lim_{x\to-\infty}\xi(x)=1$, buf if we also want absence of limits for $x\to-\infty$ just consider $\xi(x^2)$ instead.
I think I will continue using sines in throwaway counterexamples :-)