TITLE: Continuity almost everywhere
QUESTION [16 upvotes]: Let 
$$f(x) = \left\{\begin{array}{ll}
1 & \text{if }x\in\mathbb{Q},\\
0 & \text{if }x\notin\mathbb{Q}.
\end{array}\right.$$
$$g(x) = \left\{\begin{array}{ll}
\frac{1}{n} & \text{if }x = \frac{m}{n}\in\mathbb{Q},\\
0 & \text{if }x\notin\mathbb{Q}.
\end{array}\right.$$
I have some difficulties with understanding of continuity almost everywhere.
Honestly, I thought that continuity almost everywhere means that I could take any continuous function and change (or even make them undefined) its values for countable set of points from domain, say, for $\mathbb{Q}$. Based on such thinking, both $f(x), g(x)$ are continuous almost everywhere, because I could get them as a result of change of countable values of $h(x)  \equiv 0$
The fact is that only $g(x)$ is continuous almost everywhere, while $f(x)$ is discontinuous. Could you explain what went wrong in my understanding?

REPLY [4 votes]: To prove that $g$ is continuous on $\mathbb R \setminus \mathbb Q$, fix $x\in\mathbb R \setminus \mathbb Q$ and $\epsilon > 0$. Let $N = \lceil \frac1\epsilon \rceil$.
Let $a$ be such that $x\in [\frac a{M}, \frac{a+1}{M}] =: I$, chosing $M > N$ such that there is no $q = \frac nm\in\mathbb Q$ with $\gcd(m,n) = 1$ and $m < N$, therefore any
$q\in I$ has the reduced form $\frac nm$ with $m \ge N$ so $g(q) \le \frac1N \le \epsilon$ thus if be chose $\delta > 0$ such that $B_\delta(x_0) \subset I$, we have found $\delta$ with
$$|g(x) - g(x_0)| = g(x) \le \epsilon \qquad \forall x: |x-x_0| < \delta$$
qed.
Note that $x_0$ must be irrational for the existence of such an $I$ wich doesn't contain any fractions with "small" denominator.