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

Additional Power Function for Signed 64.64 Fixed Point Numbers #26

Open
mshakeg opened this issue Jul 24, 2023 · 0 comments
Open

Additional Power Function for Signed 64.64 Fixed Point Numbers #26

mshakeg opened this issue Jul 24, 2023 · 0 comments

Comments

@mshakeg
Copy link

mshakeg commented Jul 24, 2023

Currently, the library provides a pow(int128 x, uint256 y) function to calculate x raised to the power of y, where x is a signed 64.64 fixed-point number and y is an unsigned integer. This function is great for scenarios where the exponent is an integer.

However, there are use cases where it would be beneficial to have a power function where both the base and the exponent are signed 64.64 fixed-point numbers. Essentially, a function with this signature: pow(int128 x, int128 y)

Such a function would allow raising a signed 64.64 fixed-point number to the power of another signed 64.64 fixed-point number. This would be very useful for calculations involving fractional exponents, and would increase the versatility of the library.

Implementing this function might involve using the existing ln(int128 x) and exp(int128 x) functions, since:

x^y = b^(y*log_b(x))
x^y = e^(y*ln(x))
x^y = 2^(y*log_2(x))

Example implementation:

function pow(int128 x, int128 y) internal pure returns (int128) {
    require(x >= 0, "Negative base not allowed");
    if (x == 0) {
        require(y > 0, "0^0 is undefined");
        return 0;
    }
    return exp(mul(y, ln(x)));
    // OR(whichever is more gas cheap)
    return exp_2(mul(y, log_2(x)));
}
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

No branches or pull requests

1 participant