Go Down

Topic: ABS(), according to the website must be incorrect. (Read 1 time) previous topic - next topic

The website can't be correct in their explanation of the ABS() function.  It says it returns -x if x is less than 0.  However, the definition of ABS is that it ALWAYS returns a positive value.  True?

eried

If is less than 0, like -1 it returns -x, that means -(-1)
My website: http://ried.cl

MichaelMeissner


The website can't be correct in their explanation of the ABS() function.  It says it returns -x if x is less than 0.  However, the definition of ABS is that it ALWAYS returns a positive value.  True?

Actually it doesn't.  There is a corner case, that the most negative number for a given integral type has no positive counterpart, so ABS of that value, will still return a negative number.  For example, in the Arduino, the int type is 16-bits, which means the largest positive number is 32767, while the most negative number is -32768.  If you negate -32768, you will get -32768.  This is due to the use of two's complement format that integers are presented in.

If you go to sign and magnitude that is used for floating point (and some older mainframes), every negative number has a positive counterpart, but now you have the possibility of negative 0.

A third scheme that was used in the past is one's complement.

Go Up