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

Make UD60x18-pow work with values lower than 1e18 #181

Closed
PaulRBerg opened this issue Apr 7, 2023 · 0 comments · Fixed by #182
Closed

Make UD60x18-pow work with values lower than 1e18 #181

PaulRBerg opened this issue Apr 7, 2023 · 0 comments · Fixed by #182
Assignees

Comments

@PaulRBerg
Copy link
Owner

PaulRBerg commented Apr 7, 2023

The pow function in UD60x18 reverts when x is lower than 1e18, because the log2 function is used in an intermediary step, and so in-flight negative values are required.

However, it is possible to circumvent this limitation by using this mathematical trick suggested by @FoxDev12 in #176 (which is closed now, unfortunately):

x^y = 1/((1/x)^y)

In practice, the idea is to refactor the pow function to look something like this:

if (xUint > uUNIT) {
    result = exp2(mul(log2(x), y));
} else if (xUint == uUNIT) {
    result = UNIT;
} else {
    UD60x18 rX = div(UNIT, x);
    UD60x18 rResult = exp2(mul(log2(rX), y));
    result = div(UNIT, rResult);
}
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 a pull request may close this issue.

1 participant