Understanding abs()

I thought the reference to abs() was wrong at
https://www.arduino.cc/en/Reference/Abs

So I looked to contact the writer and was directed to GitHub to see if the issue was already documented and found it is.

But the answer and reasoning is vague.
Can someone clarify if what I should expect from the code below.

a = 7

b = abs(a)

b = 7 Is this correct?

a = -4

b = abs(a)

b = 4 Is this correct?

No.

a = 7

b = abs(a)

b == 7

a = -4

b = abs(a)

b == 4

Yes, provided that we understand that your equals signs above are not meant to be C++ assignment operators :stuck_out_tongue: .

Yes. abs(x) should return x if x is positive or -x if x is negative.

tip32a:
I thought the reference to abs() was wrong at
https://www.arduino.cc/en/Reference/Abs

So I looked to contact the writer and was directed to GitHub to see if the issue was already documented and found it is.

abs function incorrect return description · Issue #3338 · arduino/Arduino · GitHub

But the answer and reasoning is vague.
Can someone clarify if what I should expect from the code below.

a = 7

b = abs(a)

b = 7 Is this correct?

a = -4

b = abs(a)

b = 4 Is this correct?

abs() returns the absolute value; it will always return the positive version of the input. So yes, what you've shown is correct behavior.

Delta_G:
Yes. abs(x) should return x if x is positive or -x if x is negative.

?

UKHeliBob:
?

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.

Right. So if x is negative then it had better return -x so it can return a positive number.

UTSL

#define abs(x)  ( (x) > 0 ? (x) : -(x) )

To take the absolute value, square the number, then take the square root, then round it off. Of course, you may have precision problems.

Really, the problem is here:

I thought the reference to abs() was wrong at https://www.arduino.cc/en/Reference/Abs

Really? Could you possibly give us a teensy clue about what might be wrong with it?

So I looked to contact the writer and was directed to GitHub to see if the issue was already documented and found it is.
abs function incorrect return description · Issue #3338 · arduino/Arduino · GitHub
But the answer and reasoning is vague.

No it isn't.

From a strictly mathematical standpoint, square then square root would give two answers, one positive and one negative.

Delta_G:
Right. So if x is negative then it had better return -x so it can return a positive number.

I am lost
What should be the output from

  Serial.println(abs(123));
  Serial.println(abs(-123));

If x is negative (-42) than abs returns -x or -(-42) or 42. Capiche?

UKHeliBob:
I am lost
What should be the output from

  Serial.println(abs(123));

Serial.println(abs(-123));

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.

The enemy of my enemy is my friend. Double negative in proverb form.

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.

Why complicate things ?

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.

I guess it should say that abs() returns abs(x) when x is negative. 8^)

but I find the explanation that the function returns -x when x is negative less than helpful when what it actually returns is x.

But, that is not true. If x is -8, the function does NOT return the value of x.