温馨提示×

c++ 中如何使用对数简化计算

c++
小樊
93
2024-07-25 01:28:12
栏目: 编程语言

在C++中,可以使用标准库中的<cmath>头文件提供的log()函数来计算对数。该函数的原型如下:

double log(double x);

这个函数接受一个参数,即要计算对数的数值,返回值为该数值的自然对数(以e为底的对数)。例如,要计算以10为底的对数,可以使用如下代码:

#include <iostream>
#include <cmath>

int main() {
    double num = 100.0;
    double logarithm = log(num) / log(10.0); // 计算以10为底的对数
    std::cout << "log10(" << num << ") = " << logarithm << std::endl;

    return 0;
}

0