I’ve mentioned speed improvements in a better addition algorithm that I’m writing. I’ve already finished the parts that actually improve on the original, so I thought I’d go ahead and outline the algorithm.
Originally, this is what was done:
Input:
123.021 + 1.1
Convert input to:
123021+001100
Then loop through and add each column, carrying as necessary. The result would be 124121, and then the output function would combine that with the exponent: 124.121.
The new algorithm is based on the fact that unless both numbers have the same number of digits and the same exponent, there will be some of this “overlap”, which is what the padding eliminated in the old algorithm. But why pad with 0’s? Aren’t I just adding? Don’t I know what a number + 0 is before I calculate it? Of course.
So the new algorithm identifies which parts of the numbers would just get added to insignificant zeros (as opposed to significant zeros, like the ones in “2002″) and copies them over verbatim to the result, no calculations necessary.
For example:
Input:
123.021 + 1.1
Intermediate steps:
(copy outliers)
Result = 120.021
(perform addition, the loop described above, but only for the middle two digits)
Result = 124.121
Make sense? In this example, I cut the number of times the addition loop runs from 6 to 2! This won’t save time in all cases, but in some it will greatly improve things.
One Comment
I completely understand the need for coffee – 4 hours of sleep is WAY too many.