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

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.

?

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.

1 Like

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.

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?

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 ?

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.

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

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

That is, of course, true.

Bad English on my part

Hi,

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.

return -1*x

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.

MrAl:
Hi,

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

Regards,
Ray L.

I see the issue.
Some computers make the distinction between -0 and 0.
Read the post.

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

for all computers should be:

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

I know it is a funny to think of -0 but it does exit.
I can't think of a current day computer that still uses
-0, though.
Dwight