abs();

Hello. I read the other day about abs() function. -> http://arduino.cc/en/Reference/abs

abs(x)

Description

Computes the absolute value of a number.

Parameters
x: the number

Returns
x: if x is greater than or equal to 0.
-x: if x is less than 0.

So, as the absolute value of a number |x| is always 'x' what about that?

-x: if x is less than 0.

Thanks

I don't understand the question. If the value provided is positive, the result will be positive. If the value provided is negative, the result will be the value multiplied by negative 1.

Thanks for your reply

So wait a minute.

The absolute value of 255 is 255 -> |255| = 255
The absolute value of -255 is again 255 -> |-255| = 255

Am I wrong?

As the description says: |x| is only equal to x if x is >= 0.

Whoever told you that |x| is always equal to x is wrong. If you know vectors, think of abs() as the magnitude of the number, the distance from zero.

Unless x is unsigned, but I don't think that is what is going on here. 8^)

No, that is right. After all if |x| was always equal to x, there wouldn't be much point would there?

Yep, according to this Absolute Value in Algebra the returning value must be always positive.(as is the distance from 0)

The reference says

Returns
x: if x is greater than or equal to 0.
-x: if x is less than 0.

It is completely correct, but you are understanding it wrong.

If X is greater than or equal to 0, then it returns X. So, if X is 23 then it returns 23.
If X is less than 0 then it returns negative X. So, if X is -23 then it returns --23, which is 23 (two minuses make a plus).

Oh. I see. That's right.
Thanks for your time, guys!:slight_smile:

Jek90:
Yep, according to this Absolute Value in Algebra the returning value must be always positive.(as is the distance from 0)

Unfortunately, that is the case in an ideal world. In the real world for integer data types on modern computers, the most negative number has no positive counterpart. This is due to the use of the two's complement arithmetic scheme for integers. On a system where ints are 16-bits (i.e. AVR based Arduinos), a signed integer can range from -32768 to +32767. If you do abs of -32768 or -(-32768), you get -32768 once again.

Now for floating point, the mantissa is typically represented in a sign magnitude method, where the sign bit is separate from the mantissa part, so you can do fabs/fabsf by just clearing the sign bit. However, the wrinkle in floating point is the existence of NaN (not a number). While if you look at the bits, there are theoretically negative and positive NaN's, a NaN of either sign will act the same, and always return false in any ordered comparison.

Furthermore, in terms of floating point and signs, is the concept of negative 0 (-0.0), which is possible by setting the sign bit to 1. However, normal floating point operations will treat -0.0 like 0.0, but there are a few places where negative zero crops up (the copysign and fma functions being the two big examples, but it also crops up in some other cases with high levels of optimization on certain machines).

In terms of integer representation, historically there were machines that used 2 other representations (sign-magnitude as used on Burroughs machines, and one's complement as used on Univac and CDC machines), but generally today modern machines use two's complement.