Calculating logarithms – the HP-35 way
In 1972, Hewlett-Packard introduced the HP-35, the world’s first pocket-sized scientific calculator capable of calculating logarithmic, exponential and trigonometric functions.
What I find particularly fascinating is that it performed these calculations using essentially just addition, subtraction, shifts and a small table of precomputed constants.
The entire microcode fitted into just 768 instructions of 10 bits each, equivalent to 960 bytes!
BCD arithmetic
The HP-35 used BCD (Binary Coded Decimal) arithmetic. In BCD, each decimal digit is represented separately by four bits. For example, the number 1234 is represented as:
The advantage of this representation is that multiplication or division by 10 simply requires shifting the digits one position to the left or right, assuming a fixed decimal point.
This also makes multiplication by numbers such as 1.1, 1.01 and 1.001 particularly easy:
Suppose we want to multiply 3 by 1.1, using just two registers, A and B.
| Operation | Register A | Register B |
|---|---|---|
| Initial value | 3.000 | 0.000 |
| Copy A to B | 3.000 | 3.000 |
| Shift B right | 3.000 | 0.300 |
| Add B to A | 3.300 | 0.300 |
And there we have it: 3 \times 1.1 = 3.3, using just a copy, a shift and an addition.
The same principle applies to:
The only difference is the number of shifts required.
Multiplication by 2 is even simpler:
Calculating logarithms
The logarithmic algorithm used by the HP-35 is closely related to CORDIC (COordinate Rotation DIgital Computer) and uses techniques known as pseudo-multiplication and pseudo-division.
Interestingly, the mathematical principle behind this algorithm goes back to Henry Briggs and his radix method, published in his Arithmetica Logarithmica in 1624, almost 350 years before the HP-35 was introduced.
Let’s start with the following identity:
We need a small table of seven logarithmic constants:
| x | \ln(x) |
|---|---|
| 10 | 2.302585092994 |
| 2 | 0.693147180560 |
| 1.1 | 0.095310179804 |
| 1.01 | 0.009950330853 |
| 1.001 | 0.000999500333 |
| 1.0001 | 0.000099995000 |
| 1.00001 | 0.000009999950 |
The idea is to start with the number whose logarithm we want to calculate and, through a series of multiplications, get as close to 10 as possible without exceeding it.
Every time we multiply our number by a factor, we subtract the logarithm of that factor from our initial logarithmic value, \ln(10).
Calculating \ln(3), step by step
We start with two registers:
At each step, we perform two operations:
We repeat these operations as long as
If the multiplication would exceed 10, we move on to the next smaller factor.
The actual value of \ln(3) is:
The absolute error in the last column is defined as:
| Step | Factor F | Register X | Register L | 10-X | Absolute error |
|---|---|---|---|---|---|
| 0 | — | 3.000000000000 | 2.302585092994 | 7.000000000000 | 1.203972804326 |
| 1 | 2 | 6.000000000000 | 1.609437912434 | 4.000000000000 | 0.510825623766 |
| 2 | 1.1 | 6.600000000000 | 1.514127732630 | 3.400000000000 | 0.415515443962 |
| 3 | 1.1 | 7.260000000000 | 1.418817552825 | 2.740000000000 | 0.320205264157 |
| 4 | 1.1 | 7.986000000000 | 1.323507373021 | 2.014000000000 | 0.224895084353 |
| 5 | 1.1 | 8.784600000000 | 1.228197193217 | 1.215400000000 | 0.129584904549 |
| 6 | 1.1 | 9.663060000000 | 1.132887013412 | 0.336940000000 | 0.034274724744 |
| 7 | 1.01 | 9.759690600000 | 1.122936682559 | 0.240309400000 | 0.024324393891 |
| 8 | 1.01 | 9.857287506000 | 1.112986351706 | 0.142712494000 | 0.014374063038 |
| 9 | 1.01 | 9.955860381060 | 1.103036020853 | 0.044139618940 | 0.004423732185 |
| 10 | 1.001 | 9.965816241441 | 1.102036520520 | 0.034183758559 | 0.003424231852 |
| 11 | 1.001 | 9.975782057683 | 1.101037020187 | 0.024217942317 | 0.002424731519 |
| 12 | 1.001 | 9.985757839740 | 1.100037519854 | 0.014242160260 | 0.001425231186 |
| 13 | 1.001 | 9.995743597580 | 1.099038019521 | 0.004256402420 | 0.000425730853 |
| 14 | 1.0001 | 9.996743171940 | 1.098938024520 | 0.003256828060 | 0.000325735852 |
| 15 | 1.0001 | 9.997742846257 | 1.098838029520 | 0.002257153743 | 0.000225740852 |
| 16 | 1.0001 | 9.998742620542 | 1.098738034520 | 0.001257379458 | 0.000125745852 |
| 17 | 1.0001 | 9.999742494804 | 1.098638039519 | 0.000257505196 | 0.000025750851 |
| 18 | 1.00001 | 9.999842492229 | 1.098628039569 | 0.000157507771 | 0.000015750901 |
| 19 | 1.00001 | 9.999942490653 | 1.098618039619 | 0.000057509347 | 0.000005750951 |
After just 19 iterations, X is very close to 10, and L is very close to \ln(3).
We could continue with smaller factors to obtain more accurate digits, but there is a much more elegant solution.
Notice something interesting about the last two columns. As we progress through the iterations, the remaining difference between X and 10 becomes increasingly close to ten times the absolute error in our logarithmic result.
In other words:
Could we use this observation to improve our result without performing any further iterations?
Indeed, we can!
The final correction
For simplicity, suppose we end up with 9.99998 instead of 10.
The remaining difference is:
However, logarithms deal with relative rather than absolute changes. Since our target is 10, we divide this difference by 10:
So we would need one more multiplication, by approximately 1.000002.
Now, we know that for small values of x:
Therefore:
We can simply subtract 0.000002 from our logarithmic result.
Since dividing by 10 is just a decimal shift, all we need to do is shift the remaining difference one position to the right and subtract it from our result.
In other words:
Let’s apply this to our calculation of \ln(3).
The remaining difference is approximately (see line 19 in the table):
After shifting one decimal position to the right:
Subtracting this from L gives us:
Let’s compare the results:
| Value | |
|---|---|
| Before correction | 1.098618039619 |
| After correction | 1.098612288685 |
| Actual value | 1.098612288668 |
The absolute error has been reduced from approximately
to
An improvement by a factor of almost 348,000!
This simple trick approximately doubles the number of accurate digits, without requiring any additional logarithmic constants!
Conclusion
What I find particularly elegant is that we have calculated a natural logarithm without performing a single general multiplication or division.
All we have used is:
- Addition
- Subtraction
- Comparison
- Decimal shifts
- Seven precomputed logarithmic constants
Even more impressive is that the final correction, which provides approximately half of our accurate digits, requires just two subtractions and one decimal shift.
The example above is a simplified representation of the original HP-35 algorithm. I have deliberately omitted floating-point normalisation, finite register precision and some implementation details to make the underlying mathematical principle easier to follow.
The numerical values in the table are rounded to 12 decimal places for readability.
Not bad for an algorithm whose mathematical roots go back more than 400 years!