Paul Calnan's Blog
Published December 21, 2020

I have been reading a Calculus book recently and came across formulas for min and max:

\[ \begin{align} min(x, y) = \frac{x + y - |y - x|}{2} \\ max(x, y) = \frac{x + y + |y - x|}{2} \end{align} \]

Here \(\frac{x + y}{2}\) is the average of the two numbers or the midpoint between the two on the number line.

Also \(\frac{|y - x|}{2}\) is half the distance between y and x. Adding it to the midpoint gives the maximum (rightmost) value and subtracting it from the midpoint gives the minimum (leftmost) value.

This is particularly interesting coming from a programming background where:

min(x, y) := x <= y ? x : y
max(x, y) := x >= y ? x : y

I never thought of coming up with a non-procedural formula for either of these.

We can prove that \(min(x, y) = \frac{x + y - |y - x|}{2}\) and \(max(x, y) = \frac{x + y + |y - x|}{2}\) as follows:

If \(x \leq y\), then \(|y - x| = y - x\). So \(x + y - |y - x| = x + y - y + x = 2x = 2 \cdot min(x, y)\), and \(x + y + |y - x| = x + y + y - x = 2y = 2 \cdot max(x, y)\).

In both cases, swap \(x\) and \(y\) to prove the formulas for \(x \geq y\).