TITLE: Example of incomplete metric on $\mathbb{R}$?
QUESTION [6 upvotes]: I was wondering if the following is a valid example of an incomplete metric on $\mathbb{R}$. Let $d(x,y)$ be defined as follows: if $x,y\in\mathbb{Q}$ then $d(x,y) = |x-y|$; if $x,y\in\mathbb{R}\setminus\mathbb{Q}$ then $d(x,y) = |x-y|$; otherwise (if one is rational and the other is not), let $d(x,y) = |x-y|+1$.
We verify this is a metric. The only concern is triangle inequality. Let $x,y,z\in\mathbb{R}$, if $x,z\in\mathbb{Q}$ or $x,z\in\mathbb{R}\setminus\mathbb{Q}$, then we have
$$d(x,z)\leq d(x,y) + d(y,z)$$
regardless of if $y\in\mathbb{Q}$ or $y\in\mathbb{R}\setminus \mathbb{Q}$ since either case can only increase the RHS. On the other hand, if $x\in\mathbb{Q}$ and $z\in\mathbb{R}\setminus \mathbb{Q}$, then the LHS gets 1 added to it but the RHS does as well, so the triangle inequality holds.
For incompleteness, consider a sequence of rationals converging to $\pi$ in the Euclidean norm. Since the metric restricted to rationals is simply the Euclidean metric, this is a Cauchy sequence. However, for each $x_n$ in the sequence is rational, it cannot converge to an irrational number as $d(x_n,y)\geq 1$ for all $y\in\mathbb{R}\setminus \mathbb{Q}$. On the other hand, $x_n$ cannot converge to a rational as well: if it does, then it must converge to that rational (say $q$) in the Euclidean norm, which is a contradiction since it cannot converge to both $\pi$ and $q$ in the Euclidean norm.

REPLY [6 votes]: $\mathbb{R}$ is considered with the standard topology, so you want a metric that induces this topology and is not complete.
The standard idea is to exhibit a metric space homeomorphic to $\mathbb{R}$, but whose metric is not complete. Any open interval of $\mathbb{R}$ that is not $\mathbb{R}$ works. Now use the homeomorphism to get a metric on $\mathbb{R}$.
Examples:

$\mathbb{R} \to (0, \infty)$, $x \mapsto \exp x$. The induced metric on $\mathbb{R}$ is $d_1(x,y) = \lvert\exp x - \exp y\rvert$

$\mathbb{R} \to (-1,1)$, $x \mapsto \frac{x}{\sqrt{1+x^2}}$ (with inverse $t \mapsto \frac{t}{\sqrt{1-t^2}}$). The induced metric is $d_2(x,y) = |\frac{x}{\sqrt{1+x^2}} - \frac{y}{\sqrt{1+y^2}}|$


Note: It appears elsewhere on this site the result: $X$ non-compact metric space implies there exists a metric on $X$ that is not complete.