3
$\begingroup$

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

$\endgroup$
1
  • $\begingroup$ 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. $\endgroup$ Oct 23 '15 at 17:21
5
$\begingroup$

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

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

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.

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

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

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

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.