Recently I received a programming task: There are 2 numbers, you need to output the greatest, without using conditional and bit operators, without using branching and functions. Only integer operations are allowed. After some thought, I derived the formula: $$ \forall a,b \in\mathbb{Z}\mathop{\bullet} \max(a,b)= \frac{(a+b)+(a-b)\times\Big(\big(2\times(a-b)+1\big)\mod 2\Big)}{2}$$ i.e. it behaves as follows for any integers $a$ and $b$: $$ \max(a,b)=\left\{\begin{matrix}a,& a\gt b\\b,&a\lt b\\a,&a=b\end{matrix}\right.$$
My question is: how can we strictly proof it? I mean in this formula I replaced absolute value with an expression that uses only integer operations, but I'm not pretty sure about severity.
We can represent $$\left| n \right| = \sqrt{n^2}$$, which mean, that $$n((2n+1) mod 2) = \sqrt{n^2}$$, so $$(2n+1) mod 2 = 1$$, it's true by definition. And after that, we can represent expression $$\frac{\left|a - b\right| + \left|a+b\right|}{2}$$ without absolute values.