# Checking if a given $d(x,y)$ is a metric

For $x,y$ $\in$ $[0,1]$, let $$d(x,y)= \begin{cases} |x-y|, & \text{if x-y \in \mathbb{Q} } \\[2ex] 2, & \text{if x-y \notin \mathbb{Q}} \end{cases}$$ where $\mathbb{Q}$ is a set of rational numbers. Find if $d$ is a metric in $[0,1]$.

So what I've already done is:

1) $d(x,y)=0$ iff $x=y \Rightarrow$ $d(x,x)=|x-x|=0$ because $x-x=0 \in \mathbb{Q}$

2) $d(x,y)=d(y,x) \Rightarrow$

2.1) if $x-y \in \mathbb{Q}$ and $y-x \in \mathbb{Q}$ then $|x-y|=|y-x|$,

2.2) if $x-y \notin \mathbb{Q}$ and $y-x \notin \mathbb{Q}$ then $2=2$ - ok

2.3) if $x-y \in \mathbb{Q}$ and $y-x \notin \mathbb{Q}$ then $|x-y|=2$ $\Rightarrow x=y+2$ or $x=y-2$

2.4) if $x-y \notin \mathbb{Q}$ and $y-x \in \mathbb{Q}$ then $2=|y-x|$ $\Rightarrow x=y+2$ or $x=y-2$

3) $d(x,y) \le d(x,z) + d(z,y) \,,\forall x,y,z\in \mathbb{Q} \Rightarrow$

So do I have to write all of these and prove inequalities like: $|x-y| \le |x-z| + |z-y|$ or is there any other option?

If anyone can help me with the 3rd condition and check all the previous ones, if I didn't make any mistakes (or is it wrong?), I would be really thankful.

• For (2) I think it's better to say $d(xy)=|x-y|=|y-x|=d(y,x)$ or $d(xy)=2=d(y,x)$. Commented Nov 26, 2017 at 9:17
• Also, pay attention to your use of the implication arrow. Commented Nov 26, 2017 at 9:17
• Wow. Three attempted answers already trying to come up with counter-examples where $x,y \notin [0,1]$. Commented Nov 26, 2017 at 9:18
• For every unusual counter-examples we can apply numbers $\dfrac{x}{x+y+z}$, $\dfrac{y}{x+y+z}$, $\dfrac{z}{x+y+z}$ to be in $[0,1]$. Commented Nov 26, 2017 at 9:20
• @kahen One should always read the statements of the problems carefully before answering, right?! ;-) Commented Nov 26, 2017 at 9:22

If $x,y\in[0,1]$, then $d(x,y)=d(y,x)$ because $x-y\in\mathbb{Q}\iff y-x\in\mathbb Q$.

If $x,y,z\in[0,1]$, we have several possibilites:

• if $x-y,y-z\in\mathbb Q$, then $x-z\in\mathbb Q$ and it is clear then that $d(x,z)\leqslant d(x,y)+d(y,z)$.
• if $x-y\in\mathbb Q$ and $y-z\notin\mathbb Q$, then $x-z\notin\mathbb Q$ and$$d(x,z)=2\leqslant|x-y|+2=d(x,y)+d(y,z).$$The same argument holds if $x-y\notin\mathbb Q$ and $y-z\in\mathbb Q$.
• If $x-y,y-z\not\in\mathbb Q$ and $x-z\in\mathbb Q$, then$$d(x,z)=|x-y|\leqslant4=d(x,y)+d(y,z).$$
• If $x-y,y-z,x-z\not\in\mathbb Q$, then$$d(x,z)=2\leqslant2+2=d(x,y)+d(y,z).$$
• So I do not have to check 4 versions in $2)$? Is the first line enough to prove this condition? Commented Nov 26, 2017 at 9:26
• In order to chack that $(\forall x,y\in[0,1]):d(x,y)=d(y,x)$ all you need to do is to very that the definition of $d$ is symmetric, that is, that you exchang $x$ and $y$ nothing changes. And that's what happens, since $x-y\in\mathbb{Q}\iff y-x\in\mathbb Q$ and $|x-y|=|y-x|$. Commented Nov 26, 2017 at 9:29
• @RobertZ I don't know why, but I read $\sqrt2$. Commented Nov 26, 2017 at 9:42

Proof for the triangle inequality. We consider two cases.

1) If $x-y\in\mathbb{Q}$ then $x,y\in [0,1]$ implies that $|x-y|\leq 2$ and $|x-y|\leq d(x,y)$. Hence $$d(x,y)=|x-y|\leq |x-z|+|z-y|\leq d(x,y)+ d(y,z).$$

2) If $x-y\not\in\mathbb{Q}$ then $x-z\not\in\mathbb{Q}$ or $y-z\not\in\mathbb{Q}$. Therefore $d(x,z)=2$ or $d(y,z)=2$ and $$d(x,y)=2\leq d(x,z)+ d(y,z).$$

P.S. We can replace $2$ with any number greater or equal to the length of the interval $[0,1]$ (otherwise the triangle inequality does not hold).