lunes, 28 de julio de 2008

Subtracting without borrowing.

Petzold's book is full of excellent morsels of computer wisdom... as well as useful tips on mathematics. For example, while discussing the complexities of carrying out addition and subtraction operations on a computer, he shares with us a method to perform subtractions without borrowing (pp. 143-145):
  1. Subtract the subtrahend from a number that has as many nines as digits the numbers in the subtraction have (e.g. if the largest number in the operation is 176, you subtract that number from 999; if it is 3456, you subtract that number from 9999). This results in the nines' complement.
  2. Add the result to the original minuend.
  3. Finally, add 1 and subtract 1000.
For example, imagine we have to subtract 129 from 216. First of all, we subtract 129 from 999, which results in 870. Then, we add this result to the original minuend (e.g., 216), which gives us 1086. Finally, we add 1 (resulting in 1087) and subtract 1000, which gives us a total of 87. Now, all this is time consuming for a human being, but easier and faster to perform for a machine, and indeed that is how things work inside some of the machines we often use.

No hay comentarios: