该函数用于查找给定数字的自然对数(以e为底的对数)。
假设" x"是给定的数字:
logex = log(x);
float log(float x); double log(double x); long double log(long double x); double log(integral x);
x :这是要计算其自然对数的值。
以下是给定数字的返回值:
参数(x) | 返回值 |
---|---|
x>1 | Positive |
x=1 | 0 |
1>x>0 | Negative |
x=0 | -infinty |
x<0 | Not a Number(nan) |
让我们看一个简单的例子,当x的值为1时。
#include <iostream> #include<math.h> using namespace std; int main() { int x=1; std::cout << "Value of x is : " <<x <<std::endl; cout<<"Log value of x is : "<<log(x); return 0; }
输出:
Value of x is : 1 Log value of x is : 0
在此示例中,x的值为1。因此,函数log()返回正值,即0。
我们来看另一个简单的例子
#include <iostream> #include<math.h> using namespace std; int main() { int x=3; std::cout << "Value of x is : " <<x <<std::endl; cout<<"Log value of x is : "<<log(x); return 0; }
输出:
Value of x is : 3 Log value of x is : 1.09861
在此的示例x的值为3。因此,函数log()返回正值,即1.09861
让我们看一个简单的例子,当x的值为-0.5时。
#include <iostream> #include<math.h> using namespace std; int main() { float x= -0.5; std::cout << "Value of x is : " <<x <<std::endl; cout<<"Log value of x is : "<<log(x); return 0; }
输出:
Value of x is : -0.5 Log value of x is : nan
在此示例中,x的值为-0.5。因此,函数log()返回Not a Number(nan)。
让我们看一个简单的示例,当x的值为0时。
#include <iostream> #include<math.h> using namespace std; int main() { int x= 0; std::cout << "Value of x is : " <<x <<std::endl; cout<<"Log value of x is : "<<log(x); return 0; }
输出:
clValue of x is : 0 Log value of x is : -inf
在此的示例x的值为-1。因此,函数log()返回nan(Not a Number)。
让我们看一个简单的例子,当x的值为0.8时。
#include <iostream> #include<math.h> using namespace std; int main() { float x=0.8; std::cout << "Value of x is : " <<x <<std::endl; cout<<"Log value of x is : "<<log(x); return 0; }
输出:
Value of x is : 0.8 Log value of x is : -0.223144
在此示例中,x的值为0.8。因此,函数log()返回负值,即-0.22
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)