当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ Math log10()用法及代码示例


该函数计算给定数字的常用算法(基数为 10)。

数学上:

假设一个数字是 'x':

log10x = log10(x);

用法

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

注意:return_type 可以是 float、double 或 long double。

参数

x: 要计算其常用对数的值。

返回值

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

参数(x) 返回值
x>1 Positive
x=1 0
1>x>0 Negative
x=0 -无穷大
x<0 不是数字

例子1

我们来看一个简单的例子,当 x 的值大于 1 时。

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
    int x=5;
    std::cout << "Value of x is:" <<x <<std::endl;
    cout<<"Log value of x is:"<<log10(x);
    return 0;
}

输出:

Value of x is:5
Log value of x is:0.69897

在本例中,x 的值为 5。因此,函数 log10() 返回正值,即 0.69。

例子2

让我们看一个简单的例子,当 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:"<<log10(x);
    return 0;
}

输出:

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

在此示例中,x 的值为 1。因此,函数 log10() 返回值零。

例子3

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

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
    float x=0.3;
    std::cout << "Value of x is:" <<x <<std::endl;
    cout<<"Log value of x is:"<<log10(x);
    return 0;
}

输出:

Value of x is:0.3
Log value of x is:-0.522879

在本例中,x 的值为 0.3。因此,函数 log10() 返回负值,即 -0.52。

示例 4

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

#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:"<<log10(x);
    return 0;
}

输出:

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

在此示例中,x 的值为零。因此,函数 log10() 返回负无穷大值。

例 5

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

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
    float x= -4;
    std::cout << "Value of x is:" <<x <<std::endl;
    cout<<"Log value of x is:"<<log10(x);
    return 0;
}

输出:

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

在本例中,x 的值为 -4。因此,函数 log10() 返回 Not a Number(nan)。






相关用法


注:本文由纯净天空筛选整理自 C++ Math log10()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。