If a continuous function is positive on the rationals, is it positive almost everywhere?

I made up this question, but unable to solve it:

Let $f : \mathbb R \to \mathbb R$ be a continuous function such that $f(x) > 0$ for all $x \in \mathbb Q$. Is it necessary that $f(x) > 0$ almost everywhere?

This is my attempt.

  1. It is easy to show that $f(x) \geq 0$ everywhere, so the real question is whether $f$ can be zero at an "almost all" irrational points.

  2. The function can become $0$ at isolated points, e.g., $f(x) = (x - \sqrt{2})^2$. In particular, the qualification "almost" is necessary for the question to be nontrivial.

  3. Every rational point has an open neighborhood where $f$ is positive. Hence at least know that the set $\{ x \,:\, f(x) > 0 \}$ is not a measure-zero set.

  4. I first mistakenly assumed that Thomae's function provides a counter-example to this. Indeed, it is positive at all rationals and zero at all irrationals, but the function is continuous at only the irrationals, not everywhere.

Then I tried to prove that the question has an affirmative answer, but do not have much progress there. Please suggest some hints!


Here's a hint: If you can think of closed set $C \subset \mathbb{R}$ of positive measure containing no rationals, then the function sending $x$ to its distance from $C$ is an example.

(I changed this because I just realized you were looking for a hint)


Here's another approach to the question, in hint form:

(1) Construct a function $f(x)$ that is continuous, nonnegative, positive at $x=0$, bounded above by 1, and supported on an interval of length at most 1.

(2) Deduce that if $a,b>0$ and $c$ are real numbers, then $a f(b(x-c))$ is continuous, nonnegative, positive at $x=c$, bounded above by $a$, and supported on an interval of length at most $1/b$.

(3) Let $q_1,q_2,q_3,\dots$ be an enumeration of the rationals. Show that the function $$\sum_{j=1}^\infty 2^{-j} f\big( 2^j(x-q_j) \big)$$ is continuous, nonnegative, positive at every rational number, but equal to zero on a set of infinite measure.