声明使用:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
C库函数——log()
对数函数log()
double log(double x) 返回 x 的自然对数(基数为 e 的对数)。
log==ln(其中e为底数)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char *argv[]) {
double n;
scanf("%lf",&n);//2.7
double x = log(n);
printf("%lf",x);//0.993252
return 0;
}
公式:y=logz(x)
其中y是值,z是底数,x是指数
修改z的值则可以实现对数计算。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char *argv[]) {
double n;
scanf("%lf",&n);//1024
double x = log2(n);
printf("%lf",x);//10.000000
return 0;
}
C库函数——pow()
pow() 函数的声明。
double pow(double x, double y)//返回 x 的 y 次幂,即 x^y。
参数
- x -- 代表基数的浮点值。
- y -- 代表指数的浮点值。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char *argv[]) {
printf("%lf\n",pow(2.0, 10));
printf("%lf",pow(3.1, 2.1));
return 0;
}
结果:
2.0^10=1024.000000
3.1^2.1=10.761172