📜  C++ STL-math.log()函数

📅  最后修改于: 2020-10-18 11:49:20             🧑  作者: Mango

C++ STL math.log()

该函数用于查找给定数字的自然对数(以e为底的对数)。

数学上:

假设“ x”是给定的数字:

logex = log(x);

句法

float log(float x);
double log(double x);
long double log(long double x);
double log(integral x);

参数

×:是要计算其自然对数的值。

返回值

以下是给定数字的返回值:

Parameter(x) Return value
x>1 Positive
x=1 0
1>x>0 Negative
x=0 -infinty
x<0 Not a Number(nan)

例子1

让我们看一个简单的例子,当x的值为1时。

#include 
#include
using namespace std;
int main()
{
    int x=1;
    std::cout << "Value of x is : " <

输出:

Value of x is : 1
Log value of x is : 0

在此示例中,x的值为1。因此,函数log()返回正值,即0。

例子2

我们来看另一个简单的例子

#include 
#include
using namespace std;
int main()
{
    int x=3;
    std::cout << "Value of x is : " <

输出:

Value of x is : 3
Log value of x is : 1.09861

在此的示例x的值为3。因此,函数log()返回正值,即1.09861

例子3

让我们看一个简单的例子,当x的值为-0.5时。

#include 
#include
using namespace std;
int main()
{
    float x= -0.5;
    std::cout << "Value of x is : " <

输出:

Value of x is : -0.5
Log value of x is : nan

在此示例中,x的值为-0.5。因此,函数log()返回Not a Number(nan)。

例子4

让我们看一个简单的示例,当x的值为0时。

#include 
#include
using namespace std;
int main()
{
    int x= 0;
    std::cout << "Value of x is : " <

输出:

clValue of x is : 0
Log value of x is : -inf

在此的示例x的值为-1。因此,函数log()返回nan(Not a Number)。

例子5

让我们看一个简单的例子,当x的值为0.8时。

#include 
#include
using namespace std;
int main()
{
    float x=0.8;
    std::cout << "Value of x is : " <

输出:

Value of x is : 0.8
Log value of x is : -0.223144

在此示例中,x的值为0.8。因此,函数log()返回负值,即-0.22