且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

舍入到给定碱的最接近的整数幂

更新时间:2023-02-11 17:03:09

您已经得到了正确的想法;任何基础X, X ^楼(log_x(N))是你想要的。 (这里的 log_x 重presents日志到基本的 X 的')
在C#:

You've got the right idea; for any base x, x ^ floor( log_x(n) ) is what you want. (Where log_x represents 'log to the base x')
In C#:

static double roundBaseX(double num, double x)
{
    return Math.Pow(x, Math.Floor(Math.Log(num, x)));
}

如果你不能把对数为任意的基础,只要用公式: log_x(N)=的log(n)/日志(X)

If you can't take logarithms to an arbitrary base, just use the formula: log_x(n) = log(n) / log(x)