TITLE: Function whose image of every open interval is $(-\infty,\infty)$
QUESTION [9 upvotes]: How to find a function from reals to reals such that the image of every open interval is the whole of R?
Is there one which maps rationals to rationals?

REPLY [3 votes]: First construct a nowhere-dense perfect set (i.e. a compact set with no isolated points) $E \subset (0,1)$ that contains no rationals.  This is not hard to do by a modification of the construction of the Cantor set.  Note that $E$ has the cardinality of the continuum, and in fact it's not hard to construct a function $f_E$ from $E$ onto $\mathbb R$.
Now enumerate the intervals with rational endpoints as $(a_i, b_i)$. We can take sets $E_i \subset (a_i, b_i)$ that are pairwise disjoint, of the form $E_i = s_i + t_i E = \{s_i + t_i e: e \in E\}$ where $s_i$ and $t_i$ are rational and $t_i > 0$: in fact to choose $s_i$ and $t_i$ it suffices to have $[s_i, s_1 + t_i] \subset (a_i, b_i)$ disjoint from $\bigcup_{j=1}^{i-1} E_i$, which can be done since the latter is nowhere dense.  Now for $x \in E_i$ take $f(x) = f_E((x - s_i)/t_i)$.  Thus $f$ maps $E_i$ onto $\mathbb R$, and since every open interval contains some $E_i$ the image of such an interval is all of $\mathbb R$.  Now define $f$ however you want on the complement of $\bigcup_i E_i$ (in particular you could take $f$ to be rational on the rationals).