๐ Subtraction Through Addition โ Method of Complements
Why we invented this The method of complements exists to optimise for addition by optimising subtraction in places where there is subtraction
Computers can only add can still perform subtraction if we introduce a redundant term that can be easily removedโsuch as the next bigger place-value
Turning the subtracted term into an addition term
Thus, we have managed to generalise the problem of subtracting through addition in modular arithmetic plus removal of leading digit
๐ Example of how this works
Goal: Make Subtractions be in terms of Addition
==> A - B, where A >= B(100 - 47) is the 10's complement of 73
(-100) is the remover of the leading digit, If we extend this idea further, removing the leading digit is the same as caring about grouping numbers within a fixed range
modis a math sign invented to mean “keep only the digits within this range”
This concept is then extended for calculations involving fixed ranges, called modulo arithmetic
- In this case, removing
100from the answer is equivalent to keeping the digits within100 - So instead of
-100, we could generally do... mod 100
And since we group numbers according to a fixed range, when numbers exceed the range, it will appear to loop back to the start of this fixed range
Thus, circular properties are present in systems using fixed ranges to represent numbers.
For example, the 2’s complement system for representing binary numbers in computers
In the following sections, we will extend the explanation more generally
๐ง Successor to the Most-Significant Value
Let S(x) be the next significant place, where the S stands for Successor of place-value
i.e.
If x is some number where r is the radix, and n is highest power of that radix
Then S(x) = r^(n + 1)Where
S(13) = 100
S(99) = 100
S(123) = 1000
S(1234) = 10000๐งฎ Subtraction Setup Using S(A)
We set up MSV Redundancy by doing this
A โ B
= A โ B + S(A) โ S(A)Which allows us to convert - S(A) to mod S(A)
A โ B
= A โ B + S(A) โ S(A)
--------------------------
= [A โ B + S(A)] mod S(A)Then we group the subtractive components together, that will be used for pre-calculation
A โ B
= A โ B + S(A) โ S(A)
= [A โ B + S(A)] mod S(A)
------------------------------
= {A + [ S(A) โ B ]} mod S(A)Let S(A) โ B be the r’s complement of B, denoted B'.
$r^{n+1} - B$ is known as the Radix Complement
- We take the difference between
the number that represents the next level of groupingand thesubtrahend
A โ B
= A โ B + S(A) โ S(A)
= A โ B + S(A) mod S(A)
= {A + [ S(A) โ B ]} mod S(A)
------------------------------
= (A + B') mod S(A)We have thus converted it to just addition and the removal of the MSV
โ Subtraction through Radix Complement
A โ B
= (A + B') mod S(A)Where B' , which is S(A) - B, is the Radix Complementโโ $\boxed{ \phantom{\big(} r^{n+1} - B \phantom{\big)}}$
โ๏ธ Optimizing for Per-Place Operation
Since, we still want the convenience of removing the MSV after calculation, we will keep using the MSV redundancy technique
However, instead of taking subtraction directly from the MSV, we can instead subtract from MSV - 1 for its convenient place-value calculationโโas we simply subtract from radix - 1 at every place-value.
- The extra
1that is taken out ofMSVmust be accounted for
Here’s how it goes:
We set up MSV Redundancy by doing this
A โ B
= A โ B + S(A) โ S(A)Which allows us to convert - S(A) to mod S(A)
A โ B
= A โ B + S(A) โ S(A)
--------------------------
= [A โ B + S(A)] mod S(A)Then we set up for subtraction from MSV - 1 by balancing the 1s like so
A โ B
= A โ B + S(A) โ S(A)
= [A โ B + S(A)] mod S(A)
--------------------------------------
= {[ A โ B + S(A) - 1 ] mod S(A)} + 1We can do {... mod S(A)} + 1 because S(A) is always going to be bigger than 1, which is also the smallest place-unit that existsโr^0
S(A) is minimally going to be r^1 which is r itself
Or honestly you could just account for it before applying modulo
A โ B
= A โ B + S(A) โ S(A)
= [A โ B + S(A)] mod S(A)
= {[ A โ B + S(A) - 1 ] mod S(A)} + 1
----------------------------------------
= [{ A + [S(A) - 1 - B] } mod S(A)] + 1Where now B' = S(A) โ 1 โ B, which we will call the r - 1’s complement of B
This form: r^(n+1) - 1 - B is known as the Diminished Radix Complement,
- We take the difference between
the maximum representable value in n+1 digitsโโthe number of digits is always1more than the highest place-value powerโโand thesubtrahend
A โ B
= A โ B + S(A) โ S(A)
= [A โ B + S(A)] mod S(A)
= {[ A โ B + S(A) - 1 ] mod S(A)} + 1
= [{ A + [S(A) - 1 - B] } mod S(A)] + 1
----------------------------------------
= [ (A + B') mod S(A) ] + 1Thus, we reached the optimal setup for per-place operation that maximises addition-only operations, by pre-calculating one subtraction. And adding 1 at the end.
โ Subtraction through Diminished Radix Complement
A โ B
= [ (A + B') mod S(A) ] + 1Where B', which is S(A) - 1 - B, is the Diminished Radix Complementโโ $\boxed{ \phantom{\big(} r^{n+1} - 1 - B \phantom{\big)}}$
Conclusion
Radix Complementโโโโโโโโโ r^(n+1) - B
Diminished Radix Complementโโโโ r^(n+1) - 1 - B
- Thus we notice that also:
Diminished Radix Complement=Radix Complement-1
- Or similarly:
Radix Complement=Diminished Radix Complement+1