Pages: [1]   Go Down
Author Topic: ABS(), according to the website must be incorrect.  (Read 1028 times)
0 Members and 1 Guest are viewing this topic.
Maryland, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Edison Member
*
Karma: 32
Posts: 1233
Arduino rocks?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

My website: http://ried.cl

Ayer, Massachusetts, USA
Offline Offline
Edison Member
*
Karma: 50
Posts: 1767
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset 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
Jump to: