Main Page | See live article | Alphabetical index

Negative and non-negative numbers

A negative number is a number that is less than zero, such as -3. A positive number is a number that is greater than zero, such as 3. Zero itself is neither negative nor positive. The non-negative numbers are the positive numbers together with zero. Note that some numbers are neither negative nor non-negative, for example the imaginary unit i.

Table of contents
1 Negative numbers
2 Positive numbers
3 Non-negative numbers
4 Arithmetic involving signed numbers
5 Computing

Negative numbers

These include negative integers, negative rational numbers, negative real numbers, negative hyperreal numbers, and negative surreal numbers.

Negative integers can be regarded as an extension of the natural numbers, such that the equation x - y = z has a meaningful solution for all values of x and y. The other sets of numbers are then derived as progressively more elaborate extensions and generalizations from the integers.

Negative numbers are useful to describe values on a scale that goes below zero, such as temperature, and also in bookkeeping where they can be used to represent debts. In bookkeeping, debts are often represented by red numbers, or a number in parentheses.

Positive numbers

In the context of complex numbers positive implies real, but for clarity one may say "positive real number". Zero is not a positive number, though in computing zero is sometimes treated as though it were a positive number (due to the way that numbers are typically represented).

Non-negative numbers

A number is nonnegative if and only if it is greater than or equal to zero, i.e. positive or zero. Thus the nonnegative integers are all the integers from zero on upwards, and the nonnegative reals are all the real numbers from zero on upwards.

A real matrix A is called nonnegative if every entry of A is nonnegative.

A real matrix A is called totally nonnegative by matrix theorists or totally positive by computer scientists if the determinant of every square submatrix of A is nonnegative.

Arithmetic involving signed numbers

Addition and subtraction

For purposes of addition and subtraction, one can think of negative numbers as debts. Adding a negative number is the same as subtracting the corresponding positive number:

5 + (-3) = 5 - 3 = 2 (if you have $5 and acquire a debt of $3, then you have a net worth of $2)
-2 + (-5) = -2 - 5 = -7

Subtracting a positive number from a smaller positive number yields a negative result:

4 - 6 = -2 (if you have $4 and spend $6 then you have a debt of $2).

Subtracting a positive number from any negative number yields a negative result:

-3 - 6 = -9 (if you have a debt of $3 and spend another $6, you have a debt of $9).

Subtracting a negative is equivalent to adding the corresponding positive:

5 - (-2) = 5 + 2 = 7 (if you have a net worth of $5 and you get rid of a debt of $2, then your new net worth is $7).

Also:

(-8) - (-3) = -5 (if you have a debt of $8 and get rid of a debt of $3, then you still have a debt of $5).

Multiplication

Multiplication of a negative number by a positive number yields a negative result: (-2) · 3 = -6. The reason is that this multiplication can be understood as repeated addition: (-2) · 3 = (-2) + (-2) + (-2) = -6. Alternatively: if you have a debt of $2, and then your debt is tripled, you end up with a debt of $6.

Multiplication of two negative numbers yields a positive result: (-3) · (-4) = 12. This situation cannot be understood as repeated addition, and the analogy to debts doesn't help either. The ultimate reason for this rule is that we want the distributive law to work:

(3 + (-3)) · (-4) = 3 · (-4) + (-3) · (-4).
The left hand side of this equation equals 0 · (-4) = 0. The right hand side is a sum of -12 + (-3) · (-4); for the two to be equal, we need (-3) · (-4) = 12.

Computing

On a computer, the sign of a number (whether it is positive or negative) is usually expressed using the left-most bit. If the bit is 1, the whole number is negative, otherwise the number is not negative (zero or positive). Such an integer or variable is called signed. There are many different ways to represent numbers; see Integral data type for more information on how integers are typically represented on computers. The most common system for representing negative integers in a fixed set of bits is termed "two's complement", in which negative numbers are represented by complementing the absolute value and then adding one to the value as if it were unsigned. For example, if an integer is expressed by 8 bits,

digits  binary    actual value
0       00000000  0
1       00000001  1
....
126     01111110  126
127     01111111  127
128     10000000  -128
129     10000001  -127
130     10000010  -126
....
254     11111110  -2
255     11111111  -1

Usually, the computer's central processing unit (CPU) can use both signed and unsigned variables. In typical computer architectures there is no way to determine if a given digit is signed or unsigned at runtime because 255 and -1, for instance, appear the same in memory, and both addition, subtraction and multiplication are identical between signed and unsigned values, assuming overflow is ignored, by simply cutting off higher bits than can be stored. The datatype of the value dictates which operation should be applied.

There is a duplicate material at Computer numbering formats.


Complement

Complementing a binary number means changing all the 0s to 1s and all the 1s to 0s; a Boolean NOT on each bit. A byte, holding 8 bits, can represent the values 00000000 (0) to 11111111 (25510), if all bits are used to represent the magnitude of the number. This is called an unsigned integer.

To represent both positive and negative (signed) integers, the convention is that the most significant bit of the binary representation of the number will be used to indicate the sign of the number, rather than contributing to its magnitude; three formats have been used for representing the magnitude: sign-and-magnitude, one's complement, and two's complement, the latter being by far the most common nowadays.

Sign-and-magnitude

Sign-and-magnitude is the simplest and most like human writing forms. The MSB is set to 0 for a positive number or zero, and set to 1 for a negative number. The remaining bits in the number indicate the (positive) magnitude. Hence in a byte with only seven bits (apart from the sign bit), the magnitude can range from 0000000 (0) to 1111111 (127). Thus you can represent numbers from -12710 to +12710. -43 encoded in a byte this way is 10101011.

Ones' complement

The ones'-complement representation of a negative number is created by taking the complement of its positive counterpart. For example, negated 00101011 (43) becomes 11010100 (-43) (Notice how this is different from the sign-and-magnitude convention where the same bit pattern would be -84). The PDP-1 and UNIVAC 1100/2200 series use ones'-complement arithmetic. The range of signed numbers using one's complement in a byte is -12710 to +12710.

Two's complement

Both ones'-complement and sign-and-magnitude have two ways to represent zero: 00000000 (+0) and 11111111 (-0) in one's-complement and 10000000 in sign-and-magnitude. This is sometimes problematic (since hardware for adding and subtracting becomes more complicated, as does testing for 0).

To avoid this, and to also make integer addition simpler, the two's-complement representation is the one generally used. The two's-complement representation is created by first complementing the positive number, then adding 1 to it. Thus 00101011 (43) becomes 11010101 (-43).

In two's-complement, there is only one zero (00000000). Negating a negative number involves the same operation: complementing, then adding 1. The pattern 11111111 now represents -110 and 10000000 represents -12810; that is, the range of two's-complement integers is -12810 to +12710.

To add two two's-complement integers, treat them as unsigned numbers, add them, and ignore any potential carry over (this is essentially the great advantage that two's-complement has over the other conventions). The result will be the correct two's-complement number, unless both summands were positive and the result is negative or both summands were negative and the result is non-negative. The latter cases are referred to as "overflow" or "wrap around"; the addition cannot be carried out in 8 bit two's-complement in these cases. For example:

     00101011 (+43)     11010101 (-43)     00101011 (+43)     10011010 (-101)
   + 11010101 (-43)   + 11100011 (-29)   + 11100011 (-29)   + 10110001 (- 79)
   - - - - - - - -    - - - - - - - -    - - - - - - - -    - - - - - - - - -
     00000000 (  0)     10111000 (-72)     00001110 (+14)     01001011 (overflow)\n