EDIT:
If then
and
.
If then
and
.
Is it possible to find the absolute value of an integer using only elementary arithmetic? - Mathematics Stack Exchange
algorithm - How to compute the integer absolute value - Stack Overflow
How do I get the absolute value of an integer without using Math.abs?
Find the Absolute value of a number using simple math?
Videos
EDIT:
If then
and
.
If then
and
.
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.
Same as existing answers, but with more explanations:
Let's assume a twos-complement number (as it's the usual case and you don't say otherwise) and let's assume 32-bit:
First, we perform an arithmetic right-shift by 31 bits. This shifts in all 1s for a negative number or all 0s for a positive one (but note that the actual >>-operator's behaviour in C or C++ is implementation defined for negative numbers, but will usually also perform an arithmetic shift, but let's just assume pseudocode or actual hardware instructions, since it sounds like homework anyway):
mask = x >> 31;
So what we get is 111...111 (-1) for negative numbers and 000...000 (0) for positives
Now we XOR this with x, getting the behaviour of a NOT for mask=111...111 (negative) and a no-op for mask=000...000 (positive):
x = x XOR mask;
And finally subtract our mask, which means +1 for negatives and +0/no-op for positives:
x = x - mask;
So for positives we perform an XOR with 0 and a subtraction of 0 and thus get the same number. And for negatives, we got (NOT x) + 1, which is exactly -x when using twos-complement representation.
Set the mask as right shift of integer by 31 (assuming integers are stored as two's-complement 32-bit values and that the right-shift operator does sign extension).
mask = n>>31XOR the mask with number
mask ^ nSubtract mask from result of step 2 and return the result.
(mask^n) - mask
You can use the conditional operator and the unary negation operator:
function absVal(integer) {
return integer < 0 ? -integer : integer;
}
You can also use >> (Sign-propagating right shift)
function absVal(integer) {
return (integer ^ (integer >> 31)) - (integer >> 31);;
}
Note: this will work only with integer
Is there a way that using ONLY addition, Subtraction, Division and Multiplication, one could find the absolute value of any number? It is easy with 2, for -2 you have -2 * -2 /2 = 2. But after that I cannot figure it out. Any help would be appreciated!
There is a great trick to calculate the absolute value of a 2s-complement integer without using an if statement. The theory goes, if the value is negative you want to toggle the bits and add one, otherwise you want to pass the bits through as is. A XOR 1 happens to toggle A and A XOR 0 happens to leave A intact. So you want do something like this:
uint32_t temp = value >> 31; // make a mask of the sign bit
value ^= temp; // toggle the bits if value is negative
value += temp & 1; // add one if value was negative
In principle, you can do it in as few as three assembly instructions (without a branch). And you'd like to think that the abs() function that you get with math.h does it optimally.
No branches == better performance. Contrary to @paxdiablo's response above, this really matters in deep pipelines where the more branches you have in your code, the more likely you are to have your branch predictor get it wrong and have to roll-back, etc. If you avoid branching where possible, things will keep moving along at full throttle in your core :).
Conditionals are slower than plain arithmetic operations, but much, much faster than something as silly as calculating the square root.
Rules of thumb from my assembly days:
- Integer or bitwise op: 1 cycle
- Floating-point add/sub/mul: 4 cycles
- Floating-point div: ~30 cycles
- Floating-point exponentiation: ~200 cycles
- Floating-point sqrt: ~60 cycles depending on implementation
- Conditional branch: avg. 10 cycles, better if well-predicted, much worse if mispredicted