feat: add product ln function for UD60x18 type #227
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Unsolicited PR to add an approximation for the Lambert W-function , also known as "ProductLog" in WolframAlpha, to the UD60x18 type. The Lambert W-function is the inverse function of the transcendental equation$f(x) = x * e^x$ such that $W(f(x)) = x$ . It appears in various solutions to exponential equations, such described here.
Motivation: The Lambert W-function is needed in the
payoutFor
calculation in a Gradual Dutch Auction with a minimum price, as described in this post. Rather than add this in my project repo, I thought it may be helpful to others if contributed back to this library.Implementation: The Lambert W-function, implemented as$x \in [0, \infty)$ . This paper provides an overview of previous approximation methodologies and proposes a simple iterative method for approximating our target branch for $x > 0$ . The iteration leverages the natural logarithm, and so we rely on the existing $10^{-16}$ , with three iterations. I've implemented it here with four to try and get the extra couple decimal places, but it can potentially be dropped to three to save gas. Four iterations requires six
productLn
, is a complex function with multiple branches and is not trivial to calculate. However, since it appears in various physics domains, much work has been done developing approximations for its different branches. In the case of an unsigned integer, we only care about the "principal" real branch fromln
implementation in the library. An error analysis of the method in the paper shows that it achieves "machine precision", seemingly defined as withln
calls, which is the major factor in the gas cost of the function.Appreciate any feedback. I modeled the tests based on what was already written for the
ln
function, but happy to add additional tests or make stylistic changes if needed.