In mathematics and computing, the method of complements is a technique to encode a symmetric range of positive and negative integers in a way that they can use the same algorithm (or mechanism) for addition throughout the whole range. For a given number of places half of the possible representations of numbers encode the positive numbers, the other half represents their respective additive inverses. The pairs of mutually additive inverse numbers are called complements. Thus subtraction of any number is implemented by adding its complement. Changing the sign of any number is encoded by generating its complement, which can be done by a very simple and efficient algorithm. This method was commonly used in mechanical calculators and is still used in modern computers. The generalized concept of the radix complement (as described below) is also valuable in number theory, such as in Midy's theorem. The nines' complement of a number given in decimal representation is formed by replacing each digit with nine minus that digit. To subtract a decimal number y (the subtrahend) from another number x (the minuend) two methods may be used: In the first method, the nines' complement of x is added to y. Then the nines' complement of the result obtained is formed to produce the desired result. In the second method, the nines' complement of y is added to x and one is added to the sum. The leftmost digit '1' of the result is then discarded. Discarding the leftmost '1' is especially convenient on calculators or computers that use a fixed number of digits: there is nowhere for it to go so it is simply lost during the calculation. The nines' complement plus one is known as the tens' complement. The method of complements can be extended to other number bases (radices); in particular, it is used on most digital computers to perform subtraction, represent negative numbers in base 2 or binary arithmetic and test overflow in calculation.
Numeric complements The radix complement of an n-digit number y in radix b is defined as bn−y. In practice, the radix complement is more easily obtained by adding 1 to the diminished radix complement, which is (bn−1)−y. While this seems equally difficult to calculate as the radix complement, it is actually simpler since (bn−1) is simply the digit b−1 repeated n times in base two. This is because (see also Geometric series Formula). Knowing this, the diminished radix complement of a number can be found by complementing each digit with respect to b−1, i.e. subtracting each digit in y from b−1. The subtraction of y from x using diminished radix complements may be performed as follows. Add the diminished radix complement of x to y to obtain bn−1−x+y or equivalently bn−1−(x−y), which is the diminished radix complement of x−y. Further taking the diminished radix complement of bn−1−(x−y) results in the desired answer of x−y. Alternatively using the radix complement, x−y may be obtained by adding the radix complement of y to x to obtain x+bn−y or x−y+bn. Assuming y≤x , the result will be greater or equal to bn and dropping the leading 1 from the result is the same as subtracting bn, making the result x−y+bn−bn or just x−y, the desired result. In the decimal numbering system, the radix complement is called the ten's complement and the diminished radix complement the nines' complement. In binary, the radix complement is called the two's complement and the diminished radix complement the ones' complement. The naming of complements in other bases is similar. Some people, notably Donald Knuth, recommend using the placement of the apostrophe to distinguish between the radix complement and the diminished radix complement. In this usage, the four's complement refers to the radix complement of a number in base four while fours' complement is the diminished radix complement of a number in base 5. However, the distinction is not important when the radix is apparent (nearly always), and the subtle difference in apostrophe placement is not common practice. Most writers use one's and nine's complement, and many style manuals leave out the apostrophe, recommending ones and nines complement.
Decimal example
The nines' complement of a decimal digit is the number that must be added to it to produce 9; the nines' complement of 3 is 6, the nines' complement of 7 is 2, and so on, see table. To form the nines' complement of a larger number, each digit is replaced by its nines' complement. Consider the following subtraction problem:
873 [x, the minuend] - 218 [y, the subtrahend]
First method 1. Compute the nines' complement of the minuend (873).
999 - 873 = 126
2. Add that to the subtrahend (218).
126 [nines' complement of x = 999 - x] + 218 [y, the subtrahend] ————— 344 [999 - x + y]
3. Now calculate the nines' complement of the result
344 [result] 655 [nines' complement of 344 = 999 - (999 - x + y) = x - y, the correct answer]
Second method 1. Compute the nines' complement of 218, which is 781. Because 218 is three digits long, this is the same as subtracting 218 from 999. 2. Next, the sum of x and the nines' complement of y is taken.
873 [x] + 781 [nines' complement of y = 999 - y] ————— 1654 [999 + x - y]
3. The leading "1" digit is then dropped, giving 654.
1654 -1000 [-(999 + 1)] ————— 654 [-(999 + 1) + 999 + x - y]
4. This is not yet correct. In the first step, 999 was added to the equation. Then 1000 was subtracted when the leading 1 was dropped. So, the answer obtained (654) is one less than the correct answer x−y. To fix this, 1 is added to the answer.
654 + 1 ————— 655 [x - y]
Adding a 1 gives 655, the correct answer to our original subtraction problem. The last step of adding 1 could be skipped if instead the ten's complement of y was used in the first step.
Magnitude of numbers In the following example the result of the subtraction has fewer digits than x:
123410 [x, the minuend] - 123401 [y, the subtrahend]
Using the first method the sum of the nines' complement of x and y is
876589 [nines' complement of x] + 123401 [y] ———————— 999990
The nines' complement of 999990 is 000009. Removing the leading zeros gives 9, the desired result. If the subtrahend, y, has fewer digits than the minuend, x, leading zeros must be added in the second method. These zeros become leading nines when the complement is taken. For example:
48032 [x] - 391 [y]
can be rewritten
48032 [x] - 00391 [y with leading zeros]
… excerpt ends here. Continue reading the full article.
