固定性的对数

发布于 2025-01-29 16:03:19 字数 61 浏览 2 评论 0原文

有可能使对数坚固吗? 我已经尝试找到但没有结果。 有什么解决方案可以使对数的坚固性u Guy?感谢您的帮助。

Is possible to caculate Logarithm in Solidity?
I have try to find but have no result.
Have any solution to caculate Logarithm in Solidity u guy? Thank you for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风筝有风,海豚有海 2025-02-05 16:03:19

You can try this library: https://github.com/abdk-consulting/abdk-libraries-solidity/blob/master/ABDKMathQuad.sol

Also, here is a list Math libraries for solidity, so maybe you can find something there.

睫毛上残留的泪 2025-02-05 16:03:19

对于基本2对数:

function log2(uint x) public pure returns (uint result) {
    while (x > 1) {
        x >>= 1;
        result++;
    }
}

对于基本10对数:

function log10(uint x) public pure returns (uint result) {
    while (x > 1) {
        x /= 10;
        result++;
    }
}

最后,对于基本n对数:

function logN(uint x, uint N) public pure returns (uint result) {
    while (x > 1) {
        x /= N;
        result++;
    }
}

显然,此方法仅计算对数的整数部分。为了计算小数零件,我们必须扩展此解决方案。

For base 2 logarithm:

function log2(uint x) public pure returns (uint result) {
    while (x > 1) {
        x >>= 1;
        result++;
    }
}

For base 10 logarithm:

function log10(uint x) public pure returns (uint result) {
    while (x > 1) {
        x /= 10;
        result++;
    }
}

And finally for base N logarithm:

function logN(uint x, uint N) public pure returns (uint result) {
    while (x > 1) {
        x /= N;
        result++;
    }
}

Obviously this method calculates only integer part of logarithm. For calculating decimal parts, we must expand this solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文