C语言中的log2、log2f、log2l函数
log2、log2f 和 log2l 是 C 中的函数,用于计算给定数字的以 2 为底的对数。它们是 math.h 头文件的一部分。
句法:
#include
double log2(double x);
float log2f(float x);
long double log2l(long double x);
参数:
Function | Parameter |
---|---|
log2(x) | x => double |
log2f(x) | x => float |
log2l(x) | x => long double |
返回值:Function Return Value log2(x) log2(x) => double log2f(x) log2(x) => float log2l(x) log2(x) => long double
示例 1:下面是实现 log2(x) 的 C 程序:
C
// C program to implement
// log2(x)
#include
#include
// Driver code
int main()
{
double result, num = 67.9;
result = log2(num);
printf("log2(%lf) = %lf",
num, result);
return 0;
}
C
// C program to implement
// log2f(x)
#include
#include
// Driver code
int main()
{
float result, num = 32.256;
result = log2f(num);
printf("log2(%f) = %f",
num, result);
return 0;
}
C
// C program to implement
// log2l(x)
#include
#include
// Driver code
int main()
{
long double result, num = 73.1256;
result = log2l(num);
printf("log2(%Lf) = %LF",
num, result);
return 0;
}
输出
log2(67.900000) = 6.085340
示例 2:下面是实现 log2f(x) 的 C 程序:
C
// C program to implement
// log2f(x)
#include
#include
// Driver code
int main()
{
float result, num = 32.256;
result = log2f(num);
printf("log2(%f) = %f",
num, result);
return 0;
}
输出
log2(32.256001) = 5.011496
示例 3:下面是实现 log2l(x) 的 C 程序:
C
// C program to implement
// log2l(x)
#include
#include
// Driver code
int main()
{
long double result, num = 73.1256;
result = log2l(num);
printf("log2(%Lf) = %LF",
num, result);
return 0;
}
输出
log2(73.125600) = 6.192305