# Absolute difference

Short description: Absolute value of (x - y), a metric
Showing the absolute difference of real numbers $\displaystyle{ x }$ and $\displaystyle{ y }$ as the distance between them on the real line.

The absolute difference of two real numbers $\displaystyle{ x }$ and $\displaystyle{ y }$ is given by $\displaystyle{ |x-y| }$, the absolute value of their difference. It describes the distance on the real line between the points corresponding to $\displaystyle{ x }$ and $\displaystyle{ y }$. It is a special case of the Lp distance for all $\displaystyle{ 1\le p\le\infty }$ and is the standard metric used for both the set of rational numbers $\displaystyle{ \Q }$ and their completion, the set of real numbers $\displaystyle{ \R }$.

As with any metric, the metric properties hold:

• $\displaystyle{ |x-y|\ge 0 }$, since absolute value is always non-negative.
• $\displaystyle{ |x-y|= 0 }$   if and only if   $\displaystyle{ x=y }$.
• $\displaystyle{ |x-y|=|y-x| }$     (symmetry or commutativity).
• $\displaystyle{ |x-z|\le|x-y|+|y-z| }$     (triangle inequality); in the case of the absolute difference, equality holds if and only if $\displaystyle{ x\le y\le z }$ or $\displaystyle{ x\ge y\ge z }$.

By contrast, simple subtraction is not non-negative or commutative, but it does obey the second and fourth properties above, since $\displaystyle{ x-y=0 }$ if and only if $\displaystyle{ x=y }$, and $\displaystyle{ x-z=(x-y)+(y-z) }$.

The absolute difference is used to define other quantities including the relative difference, the L1 norm used in taxicab geometry, and graceful labelings in graph theory.

When it is desirable to avoid the absolute value function – for example because it is expensive to compute, or because its derivative is not continuous – it can sometimes be eliminated by the identity

$\displaystyle{ |x-y|\lt |z-w| }$ if and only if $\displaystyle{ (x-y)^2\lt (z-w)^2 }$.

This follows since $\displaystyle{ |x-y|^2=(x-y)^2 }$ and squaring is monotonic on the nonnegative reals.