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.
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.
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.
While this seems equally difficult to calculate as the radix complement, it is actually simpler since
Some people, notably Donald Knuth, recommend using the placement of the apostrophe to distinguish between the radix complement and the diminished radix complement.
However, the distinction is not important when the radix is apparent (nearly always), and the subtle difference in apostrophe placement is not common practice.
Consider the following subtraction problem: Compute the nines' complement of the minuend, 873.
Add that to the subtrahend 218, then calculate the nines' complement of the result.
Removing the leading zeros gives 9, the desired result.
For example: can be rewritten Replacing 00391 with its nines' complement and adding 1 produces the sum: Dropping the leading 1 gives the correct answer: 47641.
The method of complements is especially useful in binary (radix 2) since the ones' complement is very easily obtained by inverting each bit (changing '0' to '1' and vice versa).
Adding 1 to get the two's complement can be done by simulating a carry into the least significant bit.
For example: becomes the sum: Dropping the initial "1" gives the answer: 0100 1110 (equals decimal 78) The method of complements normally assumes that the operands are positive and that y ≤ x, logical constraints given that adding and subtracting arbitrary integers is normally done by comparing signs, adding the two or subtracting the smaller from the larger, and giving the result the correct sign.
For example, (in decimal): Complementing y and adding gives: At this point, there is no simple way to complete the calculation by subtracting
The expected answer is −144, which isn't as far off as it seems; 856 happens to be the ten's complement of 144.
This issue can be addressed in a number of ways: The method of complements was used in many mechanical calculators as an alternative to running the gears backwards.
For example: Use of the method of complements is ubiquitous in digital computers, regardless of the representation used for signed numbers.
However, the circuitry required depends on the representation: The method of complements was used to correct errors when accounting books were written by hand.
To remove an entry from a column of numbers, the accountant could add a new entry with the ten's complement of the number to subtract.
A bar was added over the digits of this entry to denote its special status.
It was then possible to add the whole column of figures to obtain the corrected result.
Complementing the sum is handy for cashiers making change for a purchase from currency in a single denomination of 1 raised to an integer power of the currency's base.
In grade schools, students are sometimes taught the method of complements as a shortcut useful in mental arithmetic.
The same technique works for subtracting on an adding machine.