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

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.)

• Note that there are two common definitions of remainder: I believe computer programs typically define $a \% b$, for $b>0$, to be between $-b+1$ and $0$ when $a \le 0$, and between $0$ and $b-1$ when $a \ge 0$. Whereas mathematicians would typically prefer to define $a \% b$ to be between $0$ and $b-1$ always (if they define $\%$ at all, which they typically don't). I'm not sure what a programming language would typically do in the $b \le 0$ case. Oct 23 '15 at 17:21

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$.

• Nice use of $n^2 > 0$. There may be a problem if n = -1. Oct 23 '15 at 17:35
• Pretty good, but it doesn't work for n = 0 or -1 Oct 23 '15 at 17:36
• Your latest edit nailed it. Awesome :) Oct 23 '15 at 18:30
• I think $|n|=m\%(n^2+n+2)$, which saves a step. Oct 24 '15 at 5:14

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

• Bitwise operations would work on a computer but I was looking for something that didn't delve into that realm. Oct 23 '15 at 17:29
• OK, I got it! :) Oct 23 '15 at 17:30

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$).

• No. |x| is continuous. It is not differentiable (at 0). Oct 23 '15 at 17:29
• Oops my bad, that's what I meant. Oct 23 '15 at 17:30