Is this a case of Arduino's "New" math?

Arduino Language Reference section describes abs() as follows:


Computes the absolute value of a number.


x: the number


x: if x is greater than or equal to 0.

-x: if x is less than 0.

Since when can an absolute value of a number return a negative number? I am a middle aged person; Is this the “new” math???
As described, this abs() function does nothing. Furthermore, I do not believe this is a “typo” in the description. When you use this function in a sketch, it does return a value of x as described. if x>=0, abs(x) returns x; if x<0, abs(x) returns a negative number.

What am I not understanding here?

I'm a middle-aged person too. Let's look at the description together.

If x is less than zero, then -x (which is the same as multiplying x by -1) is a positive number.

What's new or unusual about that?

( you may want to turn your keyboard over and give it a shake - looks like your '?' key is a bit sticky)

AWOL, Thanks for the clarification. What threw me off was the fact that the description stated it returns "-x". I prematurely interpreted as returning a negative number - not realizing its the negative of a negative parameter. My bad... Thanks,

What got me to investigate the abs() was when my sketch produced unexpected result. I had two unsigned int variables calculating the parameter to the abs():

unsigned int x, y;


This produced 65535 instead of 1 as I was expecting. To be fair to the Language Reference of abs(), the description does warn:


Because of the way the abs() function is implemented, avoid using other functions inside the brackets, it may lead to incorrect results.

Which I have done against.

An unsigned anything can’t be less than zero.

I'm impressed that they bothered to define exactly what the function did. As far as I can see, they never did document what the plain old div and mod operators do.

You did nothing wrong in your use of the abs function, you did not use another function inside its brackets(although you did use the abs function inside the brackets of another function). The problem was caused by declaring x and y as unsigned int. Since an unsigned integer can not be negative, 3 - 4 rolls over and gives the result 65535.

hanlee: Serial.println(abs(x-y));

Did you take the time to actually read the documentation page, or just criticize it?

From the abs() reference page: Warning Because of the way the abs() function is implemented, avoid using other functions inside the brackets, it may lead to incorrect results.

abs(a++);   // avoid this - yields incorrect results

a++;          // use this instead -
abs(a);       // keep other math outside the function

The abs() function is defined to compute the absolute value of an int and return an int result. If used in a different context (with an unsigned instead of an int, in this case) the behavior is "undefined".

A workaround might be to cast both x and y to int type: abs((int)x - (int)y)

...but a better answer would probably be to declare x and y as ints - unless of course you've found a was to produce temperatures outside the range of a signed int. ;-)

'abs' is a macro and macros can lead to expansion side effects.

Instead you may want to use 'fabs' (floating point abs) which is not macro based.