9

How do I find and use any existing Solidity libraries like you might in Javascript?

For example, Math functions: How do I implement the equivalent of Math.sqrt(x) in a solidity function?

Related: What is --libraries doing in that second question (if relevant here)?

I looked at the docs and answers below, but they didn't seem to answer this. Thanks.

How to import solidity libraries in mix?

Out of gas error deploying library

1
8

Currently the standard library isn't fully set up yet, but in this case what you would do would be to link your contracts to your library's address (and hopefully map it to a named registration in that case). That will be implemented in the future and it will be much easier when that time comes.

Until then however, you will have to do it manually, ie the hard way.

One thing you would do would be to pull the square root function from this dapp bin (though frankly it's not worth using at this point since the fixed point variable has yet to be set up, but we'll use it for this example)

https://github.com/ethereum/dapp-bin/blob/master/library/math.sol

you would do something along the lines of

import "math.sol"

contract myContract {
    function f() {
        int a = Math.sqrt(9);
    }
}

from there the --libraries flag enabled you to link to certain addresses where the argument is the name of the library followed by the address so if you have the math library at address 0x123456, you would name it:

solc yourContract.sol --libraries Math: 0x123456
1
0

My advanced math library PRBMath offers sqrt, among many other functions. I'm gonna paste my implementation here, for posterity, but check out the linked repo for the most up-to-date code.

/// @notice Calculates the square root of x, rounding down.
/// @dev Uses the Babylonian method https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method.
/// @param x The uint256 number for which to calculate the square root.
/// @return result The result as an uint256.
function sqrt(uint256 x) internal pure returns (uint256 result) {
    if (x == 0) {
        return 0;
    }

    // Calculate the square root of the perfect square of a power of two that is the closest to x.
    uint256 xAux = uint256(x);
    result = 1;
    if (xAux >= 0x100000000000000000000000000000000) {
        xAux >>= 128;
        result <<= 64;
    }
    if (xAux >= 0x10000000000000000) {
        xAux >>= 64;
        result <<= 32;
    }
    if (xAux >= 0x100000000) {
        xAux >>= 32;
        result <<= 16;
    }
    if (xAux >= 0x10000) {
        xAux >>= 16;
        result <<= 8;
    }
    if (xAux >= 0x100) {
        xAux >>= 8;
        result <<= 4;
    }
    if (xAux >= 0x10) {
        xAux >>= 4;
        result <<= 2;
    }
    if (xAux >= 0x8) {
        result <<= 1;
    }

    // The operations can never overflow because the result is max 2^127 when it enters this block.
    unchecked {
        result = (result + x / result) >> 1;
        result = (result + x / result) >> 1;
        result = (result + x / result) >> 1;
        result = (result + x / result) >> 1;
        result = (result + x / result) >> 1;
        result = (result + x / result) >> 1;
        result = (result + x / result) >> 1; // Seven iterations should be enough
        uint256 roundedDownResult = x / result;
        return result >= roundedDownResult ? roundedDownResult : result;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.