用哪个函数可以计算log2(n) ?????——在线等待

iedge 2004-05-06 02:36:00
如题
...全文
4748 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
iedge 2004-05-06
  • 打赏
  • 举报
回复
哈哈,一时忘了,谢啦
cngdzhang 2004-05-06
  • 打赏
  • 举报
回复
#include <math.h>

log是基于自然对数e的
log10是基于底数10的

要做底数为2的:

log2(x)=log(x)/log(2);


例子:

#include <stdio.h>
#include <math.h>

int main(void)
{
printf("log2(128)=%lf",log(128)/log(2));
return 0;
}

输出结果为:

log2(128)=7.0000000
freefalcon 2004-05-06
  • 打赏
  • 举报
回复
似乎也可以通过级数公式自己计算?
楼上的可以了
languagec 2004-05-06
  • 打赏
  • 举报
回复
log2(n) =lg(n)/lg(2) 的呀
而lg(n) 的函数是有的

函数名: log
功 能: 对数函数ln(x)
用 法: double log(double x);
程序例:

#include <math.h>
#include <stdio.h>

int main(void)
{
double result;
double x = 8.6872;

result = log(x);
printf("The natural log of %lf is %lf\n", x, result);

return 0;
}



改了一下,结果还正确
#include <math.h>
#include <stdio.h>

int main(void)
{
double result1,result2;
result1 = log(9);
result2 =log(3);
printf("The natural log is %lf\n", result1/result2);

return 0;
}


iedge 2004-05-06
  • 打赏
  • 举报
回复
up

24,860

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧