Skip to content

A simplified implementation of function _cbrt #107

Description

@barakman

For the calculation of a cubic root, which is implemented in the math library of twocryptoswap and probably in a few other places, consider a simplified version which by and large performs the same number of operations (i.e., consumes the same amount of gas):

def _cbrt(n: uint256) -> uint256:
    if (n > 84):
        x: uint256 = n
        y: uint256 = 1 << unsafe_add(unsafe_div(_snekmate_log_2(n, False), 3), 1)
        while (x > y):
            x = y
            y = unsafe_div(unsafe_add(x << 1, unsafe_div(n, unsafe_mul(x, x))), 3)
        return x
    return 0x49249249249249246db6db6db6db6db6db6db6db6db692492492492492249248 >> unsafe_mul(n, 3) & 0x7

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions