In order to solve the equation
the BKM algorithm takes advantage of a basic property of logarithms
Using Pi notation, this identity generalizes to
Because any number can be represented by a product, this allows us to choose any set of values a k {\displaystyle a_{k}} which multiply to give the value we started with. In computer systems, it's much faster to multiply and divide by multiples of 2, but because not every number is a multiple of 2, using a k = 1 + 2 m {\displaystyle a_{k}=1+2^{m}} is a better option than a more simple choice of a k = 2 m {\displaystyle a_{k}=2^{m}} . Since we want to start with large changes and get more accurate as k {\displaystyle k} increases, we can more specifically use a k = 1 + 2 − k {\displaystyle a_{k}=1+2^{-k}} , allowing the product to approach any value between 1 and ~4.768, depending on which subset of a k {\displaystyle a_{k}} we use in the final product. At this point, the above equation looks like this:
This choice of a k {\displaystyle a_{k}} reduces the computational complexity of the product from repeated multiplication to simple addition and bit-shifting depending on the implementation. Finally, by storing the values ln ( 1 + 2 − k ) {\displaystyle \ln(1+2^{-k})} in a table, calculating the solution is also a simple matter of addition. Iteratively, this gives us two separate sequences. One sequence approaches the input value x {\displaystyle x} while the other approaches the output value ln ( x ) = y {\displaystyle \ln(x)=y} :
x k = { 1 if k = 0 x k − 1 ⋅ ( 1 + 2 − k ) if x k would be ≤ x x k − 1 otherwise {\displaystyle x_{k}={\begin{cases}1&{\text{if }}k=0\\x_{k-1}\cdot (1+2^{-k})&{\text{if }}x_{k}{\text{ would be}}\leq x\\x_{k-1}&{\text{otherwise}}\end{cases}}}
Given this recursive definition and because x k {\displaystyle x_{k}} is strictly increasing, it can be shown by induction and convergence that
for any 1 ≤ x ≲ 4.768 {\displaystyle 1\leq x\lesssim 4.768} . For calculating the output, we first create the reference table
Then the output is computed iteratively by the definition y k = { 0 if k = 0 y k − 1 + A k if x k would be ≤ x y k − 1 otherwise {\displaystyle y_{k}={\begin{cases}0&{\text{if }}k=0\\y_{k-1}+A_{k}&{\text{if }}x_{k}{\text{ would be}}\leq x\\y_{k-1}&{\text{otherwise}}\end{cases}}} The conditions in this iteration are the same as the conditions for the input. Similar to the input, this sequence is also strictly increasing, so it can be shown that
for any 0 ≤ y ≲ 1.562 {\displaystyle 0\leq y\lesssim 1.562} .
Because the algorithm above calculates both the input and output simultaneously, it's possible to modify it slightly so that y {\displaystyle y} is the known value and x {\displaystyle x} is the value we want to calculate, thereby calculating the exponential instead of the logarithm. Since x becomes an unknown in this case, the conditional changes from
to
To calculate the logarithm function (L-mode), the algorithm in each iteration tests if x n ⋅ ( 1 + 2 − n ) ≤ x {\displaystyle x_{n}\cdot (1+2^{-n})\leq x} . If so, it calculates x n + 1 {\displaystyle x_{n+1}} and y n + 1 {\displaystyle y_{n+1}} . After N {\displaystyle N} iterations the value of the function is known with an error of Δ ln ( x ) ≤ 2 − N {\displaystyle \Delta \ln(x)\leq 2^{-N}} .
Example program for natural logarithm in C++ (see A_e for table):
Logarithms for bases other than e can be calculated with similar effort.
Example program for binary logarithm in C++ (see A_2 for table):
The allowed argument range is the same for both examples (1 ≤ Argument ≤ 4.768462058…). In the case of the base-2 logarithm the exponent can be split off in advance (to get the integer part) so that the algorithm can be applied to the remainder (between 1 and 2). Since the argument is smaller than 2.384231…, the iteration of k can start with 1. Working in either base, the multiplication by s can be replaced with direct modification of the floating point exponent, subtracting 1 from it during each iteration. This results in the algorithm using only addition and no multiplication.
To calculate the exponential function (E-mode), the algorithm in each iteration tests if y n + ln ( 1 + 2 − n ) ≤ y {\displaystyle y_{n}+\ln(1+2^{-n})\leq y} . If so, it calculates x n + 1 {\displaystyle x_{n+1}} and y n + 1 {\displaystyle y_{n+1}} . After N {\displaystyle N} iterations the value of the function is known with an error of Δ exp ( x ) ≤ 2 − N {\displaystyle \Delta \exp(x)\leq 2^{-N}} .
Example program in C++ (see A_e for table):