Delta_G:
What's the question? That's the standard absolute value since I learned it in the third grade.
Don't be confused by the -x if x is negative. Again, third grade math. -(-3) == 3 right. Taking the negative of a negative number gives a positive number.
I think his point was that absolute value never returns a negative.
Delta_G:
In the first case x is 123. That's positive so just return it. Return value is 123.
In the second case x is -123. Since x is negative return -x. -(-123) is 123. So return value is 123.
You have to remember that the -x looks like it is a negative number but it isn't. It's positive because x is negative.
This is basic pre-algebra.
No surprise that in both cases the answer is 123. It is exactly what I would expect, but I find the explanation that the function returns -x when x is negative less than helpful when what it actually returns is x.
Delta_G:
The important thing to remember is that a negative sign doesn't make a number negative. It reverses the sign on a number. Positive becomes negative and negative becomes positive.
Don't confuse the unary '-' operator with the negative sign. One converts a number, and the other merely reports a state. And don't forget the unary '+' operator that converts a positive number to positive and a negative number to negative. 8^)
After reading through this thread i have to chuckle just a little, but then again some of the simplest things are the hardest to explain.
There is another view we can explore.
Since the abs() function must return the positive version of the number whether it is already positive or it is negative, we can simply check to see if it is positive and if it is then return the original number, but if it is negative we can multiply it by -1 and that will change it's sign to positive. So in short pseudo code:
function abs(x)
if x<0 then
return -1*x
else
return x
end if
end function
In the above the intent is to make it a little more clear what is happening by using -1 as a multiplier instead of just the minus sign alone.
I think that those of us that are not confused by the abs() documentation assume that this is what "returns -x" means. It is good to state this more explicitly, though, as you have done.
After reading through this thread i have to chuckle just a little, but then again some of the simplest things are the hardest to explain.
There is another view we can explore.
Since the abs() function must return the positive version of the number whether it is already positive or it is negative, we can simply check to see if it is positive and if it is then return the original number, but if it is negative we can multiply it by -1 and that will change it's sign to positive. So in short pseudo code:
function abs(x)
if x<0 then
return -1*x
else
return x
end if
end function
In the above the intent is to make it a little more clear what is happening by using -1 as a multiplier instead of just the minus sign alone.
The meaning of "-x" is 100% unambiguous, standard mathematical notation, and should be crystal clear to anyone who has taken the first day of high-school Algebra. It is 100% equivalent to writing "x * -1".