This cosument formally proves maximum absolute errors of approximated logarithm calculation to be used in Uniswap v2 protocol.
We approximate base-2 logarithm iteratively. Initial approximation for is defined like this:
So the initial approximation is just the logarithm rounded down, i.e. towards . The absolute error of the initial approximation is bounded like this:
Every next approximation is defined like this:
Here means rounded down to the closest factor of .
The absolute error of this approximation is:
Let's note, that
thus
then
and finally:
Let's pick such that
Let's pick such that
Now we have:
We approximate base-1.01 logarithm like this:
Where . The error of such approximation is:
For the approximation error is within the following range:
We approximate base- logarithm like this:
Where . The error of such approximation is:
For the approximation error is within the following range:
Function approximates with relative pricision not worse than . Thus:
where . By inverting this function we have:
Here is some number. Note, that while actual function may be calculated only for integer arguments, we assume that it is consinuously and strictly-monotonically defined for all real numbers. This assumtion allows us to consider inverse function to be defined for all positive arguments.
We approximate the inverse function like this:
The absoulte error of such approximation is:
For the error is within the following range:
Here and are contributed by the imperfection of value; is contributed by the finity of ; is contributed by the rounding errors inside the approximation algorithm implementation (the implementation uses 129.127-bit binary fixed point numbers, thus thing); finally and are contributed by the imperfection of implementation.
Function approximates with relative pricision not worse than . Thus:
where . By inverting this function we have:
Here is some number. Note, that while actual function may be calculated only for integer arguments, we assume that it is consinuously and strictly-monotonically defined for all real numbers. This assumtion allows us to consider inverse function to be defined for all positive arguments.
We approximate the inverse function like this:
The absoulte error of such approximation is:
For the error is within the following range:
Here and are contributed by the imperfection of value; is contributed by the finity of ; is contributed by the rounding errors inside the approximation algorithm implementation (the implementation uses 129.127-bit binary fixed point numbers, thus thing); finally and are contributed by the imperfection of implementation.
What we need is to calculate for given positive value the maximum integer such that . Basically we need to solve the following inequation in integer numbers:
or
Thus:
Once we know, that , then . When there are at most two integer values fitting into range , thus the right value could be chosen by calling function at most once.
The following table gives possible ranges of absolute approximation error of for :
For at most one call to is needed to return bit-perfect value. The probability of call is shown in the last column.
What we need is to calculate for given positive value the maximum integer such that . Basically we need to solve the following inequation in integer numbers:
or
Thus:
Once we know, that , then . When there are at most two integer values fitting into range , thus the right value could be chosen by calling function at most once.
The following table gives possible ranges of absolute approximation error of for :
86% | |||
44% | |||
23% | |||
12% | |||
7% | |||
4% | |||
3% |
For at most one call to is needed to return bit-perfect value. The probability of call is shown in the last column.