Log base n of x [closed]

柔情痞子 提交于 2019-12-12 13:21:01

问题


I have a little problem. Who knows how we can calculate the log base n with Shift_L or Shift_R?

for example: for n=2 we had this solution:

int log(int n){
int res = 0;
while((n>>=1))
    res++;
return res;
}

回答1:


You don't seem to want the logarithm for a base b, but the largest integer n so that n <= log_b(x). If that's the case, the following function should serve your needs:

int intlog(double base, double x) {
    return (int)(log(x) / log(base));
}



回答2:


well this is rather a math problem instead of an actuall programming problem, if i understand your problem correctly:

log_2 (x) = log_a (x) / log_a (2) where a can be any base.

Therefore you could use the math.h's function log(double)

double res = log(x)/log(2);



来源:https://stackoverflow.com/questions/18874255/log-base-n-of-x

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!