Skip to content

Additional Power Function for Signed 64.64 Fixed Point Numbers #26

@mshakeg

Description

@mshakeg

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)));
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions