跳到主要内容

C++ log10() 以10为底的对数函数

这个函数定义在 <cmath> 头文件中。

[Mathematics] log10x = log10(x) [In C++ Programming]

log10() 函数原型 [C++ 11 标准起]

double log10 (double x);
float log10 (float x);
long double log10 (long double x);
double log10 (T x); // 对于整数类型

log10() 参数

log10() 函数接受一个范围在 [0, ∞] 内的必需参数。

如果值小于 0,log10() 返回 NaN(非数字)。

log10() 返回值

log10() 函数返回一个数的以 10 为底的对数。

参数 (x)返回值
x > 1正值
x = 10
0 > x > 1负值
x = 0-∞ (负无穷大)
x < 0nan (非数字)

示例 1:log10() 如何工作?

#include <iostream>
#include <cmath>
using namespace std;

int main ()
{
double x = 13.056, result;
result = log10(x);
cout << "log10(x) = " << result << endl;

x = -3.591;
result = log10(x);
cout << "log10(x) = " << result << endl;

return 0;
}

当你运行程序时,输出将会是:

log10(x) = 1.11581
log10(x) = nan

示例 2:对整数类型使用 log10()

#include <iostream>
#include <cmath>
using namespace std;

int main ()
{
int x = 2;
double result;

result = log10(x);
cout << "log10(x) = " << result << endl;

return 0;
}

当你运行程序时,输出将会是:

log10(x) = 0.30103