Is it possible to find the absolute value of an integer using only elementary arithmetic?

2.3k Views Asked by At

Using only addition, subtraction, multiplication, division, and "remainder" (modulo), can the absolute value of any integer be calculated?

To be explicit, I am hoping to find a method that does not involve a piecewise function (i.e. branching, if, if you will.)

3

There are 3 best solutions below

4
On BEST ANSWER

EDIT:
$$m=n\%(n^2-n+2)\\ p=m\%(n^2+2)\\ |n|=2p-n$$

If $n\ge0$ then $m=n$ and $p=n$.
If $n<0$ then $m=n^2+2$ and $p=0$.

2
On

I'm going to go with no.

My (not super rigorous) proof would be that any function of the variable $x$ using only addition, subtraction, multiplication, and division would have to be a differentiable function on its domain, but $|x|$ is not a differential function on all of its domain, and its domain is all of $\mathbb{R}$.

The introduction of an expression like $(x\mod 4)$ would introduce points of discontinuity (and hence not differential), but it would introduce an infinite amount of them (at all multiples of four), and the function $|x|$ only has one point of not being differentiable (at $x=0$).

2
On

I think you need this: https://stackoverflow.com/questions/9772348/get-absolute-value-without-using-abs-function-nor-if-statement.

Please note that remainder is modulo, absolute is modulus, so please correct the question, because I can't suggest edit.