C 语言 log()

log() 函数定义在 math.h 头文件中。它有助于返回给定参数值的自然对数(以 e 为底的对数)。


double log(double x); #where x should be in double

此外,logf() 和 logl() 函数分别用于浮点型和长双精度型。


float logf(float x); 
long double logl(long double x); 

log() 参数

log() 函数接受一个双精度浮点型参数。

参数 描述 必需/可选
x 需要找到其自然对数的值 必需

log() 返回值

log() 函数的返回值是一个浮点数。

输入 返回值
x > 0 计算参数的对数
x < 0 显示运行时错误

log() 示例

示例 1:C 语言中 log() 函数的工作原理?


#include <stdio.h>
#include <math.h>
int main()
{
    double N = 5.6, output;

    output = log(N);
    printf("The value of log(%.1f) is %.2f", N, output);

    return 0;
}

输出


The value of log(5.6) is 1.72

示例 2:C 语言中 log() 函数是如何工作的?


#include <stdio.h>
#include <math.h>
int main () {
   double k, out;
   k = 2.7;
   out = log(k);
   printf("The value of log(%lf) is %lf", k, out);
   return(0);

}

输出


The value of log(2.700000) = 0.993252