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.
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.
UKHeliBob:
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.
Why complicate things ?
What should it say? It isn't complicated at all. It's math. A programmer had better be able to handle simple math. Written like it is it is simple and unambiguous. It only causes problems for people who slept through math class and I'm sorry to say but they got bigger confusion than this coming.
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.