log(x)函数详解

首页 / C++入门教程 / log(x)函数详解

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

假设" x"是给定的数字:

logex = log(x);

log - 语法

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

log - 参数

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

log - 返回值

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

参数(x) 返回值
x>1 Positive
x=1 0
1>x>0 Negative
x=0 -infinty
x<0 Not a Number(nan)

log - 例子1

让我们看一个简单的例子,当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。

log - 例子2

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

#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

链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-math-log-function.html

来源:LearnFk无涯教程网

log - 例子3

让我们看一个简单的例子,当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)。

log - 例子4

让我们看一个简单的示例,当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)。

log - 例子5

让我们看一个简单的例子,当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

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

消息队列高手课 -〔李玥〕

浏览器工作原理与实践 -〔李兵〕

即时消息技术剖析与实战 -〔袁武林〕

分布式协议与算法实战 -〔韩健〕

深入剖析Java新特性 -〔范学雷〕

深入C语言和程序运行原理 -〔于航〕

说透元宇宙 -〔方军〕

结构执行力 -〔李忠秋〕

程序员职业规划手册 -〔雪梅〕

好记忆不如烂笔头。留下您的足迹吧 :)