Pages: [1]   Go Down
 Author Topic: ABS(), according to the website must be incorrect.  (Read 796 times) 0 Members and 1 Guest are viewing this topic.
Maryland, USA
Offline
Newbie
Karma: 0
Posts: 4
 « on: November 29, 2012, 01:54:36 pm » Bigger Smaller Reset

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?
 Logged

Chile
Offline
Edison Member
Karma: 28
Posts: 1146
Arduino rocks
 « Reply #1 on: November 29, 2012, 02:14:06 pm » Bigger Smaller Reset

If is less than 0, like -1 it returns -x, that means -(-1)
 Logged

My homepage/blog: http://erwin.ried.cl

Ayer, Massachusetts, USA
Offline
Edison Member
Karma: 27
Posts: 1097
 « Reply #2 on: November 29, 2012, 07:25:59 pm » Bigger Smaller Reset

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

 Pages: [1]   Go Up