Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add product ln function for UD60x18 type #227

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

Oighty
Copy link

@Oighty Oighty commented Apr 28, 2024

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 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 from $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 ln implementation in the library. An error analysis of the method in the paper shows that it achieves "machine precision", seemingly defined as with $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 ln 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.

@Oighty Oighty changed the title feat: add product ln function for UD60x18 values feat: add product ln function for UD60x18 type Apr 28, 2024
@PaulRBerg
Copy link
Owner

Hey, thank you for your PR and feature request. Just wanted to say that I unfortunately don't and won't have time to review this in the near future. Have added to my backlog, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants