I'm supposed to get the maximum of two unknown numbers.

I used this equation to do this task: $${x+y\over 2} + {\vert x-y\vert\over 2} $$ Let's get the maximum by substituting any two values e.g. $$x = 5$$ and $$y = 10$$ So:
$${5+10\over 2} + {\vert 5-10\vert\over 2} = 7.5 + 2.5 = 10 $$
My questions are:

  • What's the mathematical explanation behind this?
  • How can I simplify this equation?
    I can't simplify it because absolute value is confusing.
  • $\begingroup$ It helps as well to remember that $|a|=\begin{cases}a&\text{if}~a\geq 0\\ -a&\text{otherwise}\end{cases}$ $\endgroup$ – JMoravitz Mar 12 '18 at 21:52
  • 1
    $\begingroup$ Define "simplify." [math.stackexchange.com/a/1471476/123905 ] $\endgroup$ – Eric Towers Mar 12 '18 at 23:03

You are adding two values together $${x+y\over 2} + {\vert (x-y)\vert\over 2}$$

The first is the average which gives you the midpoint.

The second part is half the total distance between $x$ and $y.$

What happens if you start at the midpoint and add half the distance to that? You get to the larger one.

If you subtract the half distance you get to the smaller one.

Thus you can also find the minimum by changing the plus sign to a minus sign.

$${x+y\over 2} -{\vert (x-y)\vert\over 2}$$


It helps to keep in mind that $\,\max(x,y), \min(x,y)\,$ are just the same numbers $\,x,y\,$, but possibly in a different order. Then it follows from definitions that:

$$ \begin{cases} \begin{align} x + y &= \max(x,y)+\min(x,y) \\ |x - y| &= \max(x,y)-\min(x,y) \\ \end{align} \end{cases} $$

Adding the two identities above gives the posted relation, while subtracting them gives its dual: $\;\dfrac{x+y}{2} - \dfrac{|x-y|}{2} = \min(x,y)\,$.

  • $\begingroup$ Is it possible to simplify this formula? $\endgroup$ – Mohamed Magdy Mar 12 '18 at 22:19
  • $\begingroup$ @MohamedMagdy "Simplify" in what sense? It doesn't get much simpler than $\,\max(x,y)\,$ which is where you started to begin with. $\endgroup$ – dxiv Mar 12 '18 at 22:20
  • $\begingroup$ I can simplify it in this formula:( x+y + abs(x-y) ) / 2, but can I simplify it more than that? $\endgroup$ – Mohamed Magdy Mar 12 '18 at 22:22
  • $\begingroup$ @MohamedMagdy Again, define "simplify". At face value, $\,\dfrac{x+y + |x-y|}{2}\,$ looks a lot less simple to me than just $\,\max(x,y)\,$. $\endgroup$ – dxiv Mar 12 '18 at 22:24
  • 1
    $\begingroup$ @MohamedMagdy $\,\max\,$ is defined as $\,\max(x,y)=\begin{cases}x \quad\quad \text{if} \;x \ge y \\ y \quad\quad \text{if} \;x \lt y\end{cases}\,$. That's a piecewise-defined function and is a perfectly valid definition for a function. Same goes for the absolute value function $\,|\cdot|\,$. You could technically write $\,|a| = \sqrt{a^2}\,$ for example, so that $\,\max(x,y)=\dfrac{1}{2}\left(x+y+\sqrt{(x-y)^2}\right)\,$, but it would make no math sense to do so. $\endgroup$ – dxiv Mar 12 '18 at 22:33

Hint: You can find the secret by considering two cases if $x \geq y$ and $x < y$.

If $x \geq y$ means $\max(x,y)= x$:

$$\frac{x+y}{2} + \frac{x-y}{2} = x$$

If $x < y$ means $\max(x,y)= y$:

$$\frac{x+y}{2} + \frac{y-x}{2} = y$$

Indeed maximum changes the sign of the abs, and the minimum value will be removed in the summation.


The first term of the sum is just the average value of the two given inputs. To get the max input out of the sum, it follows that the second term of the sum gives you the difference between the average and the max (or min) input. The formula does this by finding the difference between the two inputs (using absolute values to avoid negative values) and dividing that value by two to give the “length” of the distance between the max and the average.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.