Namespaces
Variants

std:: log10, std:: log10f, std:: log10l

From cppreference.net
Common mathematical functions
Nearest integer floating point operations
(C++11)
(C++11)
(C++11) (C++11) (C++11)
Floating point manipulation functions
(C++11) (C++11)
(C++11)
(C++11)
Classification and comparison
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Types
(C++11)
(C++11)
(C++11)
Macro constants
定义于头文件 <cmath>
(1)
float log10 ( float num ) ;

double log10 ( double num ) ;

long double log10 ( long double num ) ;
(C++23 前)
/*floating-point-type*/
log10 ( /*floating-point-type*/ num ) ;
(C++23 起)
(C++26 起为 constexpr)
float log10f ( float num ) ;
(2) (C++11 起)
(C++26 起为 constexpr)
long double log10l ( long double num ) ;
(3) (C++11 起)
(C++26 起为 constexpr)
SIMD 重载 (C++26 起)
定义于头文件 <simd>
template < /*math-floating-point*/ V >

constexpr /*deduced-simd-t*/ < V >

log10 ( const V & v_num ) ;
(S) (C++26 起)
额外重载 (C++11 起)
定义于头文件 <cmath>
template < class Integer >
double log10 ( Integer num ) ;
(A) (C++26 起为 constexpr)
1-3) 计算 num 常用(底数为 10 )对数 标准库为所有 cv 未限定浮点类型提供了 std::log10 的重载版本作为参数类型。 (C++23 起)
S) SIMD 重载对 v_num 执行逐元素的 std::log10 运算。
(定义参见 math-floating-point deduced-simd-t 。)
(since C++26)
A) 为所有整数类型提供了额外的重载,这些类型被视为 double
(since C++11)

目录

参数

num - 浮点数或整数值

返回值

若无错误发生,则返回 num 的常用(以 10 为底)对数( log 10 (num) lg(num) )。

如果发生定义域错误,将返回一个由实现定义的值(在支持 NaN 的情况下返回 NaN)。

如果出现极点错误,将返回 -HUGE_VAL -HUGE_VALF -HUGE_VALL

错误处理

错误报告方式遵循 math_errhandling 中的规范。

num 小于零时会发生定义域错误。

num 为零时可能出现极点错误。

如果实现支持 IEEE 浮点算术 (IEC 60559),

  • 若参数为 ±0,则返回 -∞ 并引发 FE_DIVBYZERO
  • 若参数为 1,则返回 +0。
  • 若参数为负数,则返回 NaN 并引发 FE_INVALID
  • 若参数为 +∞,则返回 +∞。
  • 若参数为 NaN,则返回 NaN。

注释

额外的重载不需要完全按照 (A) 的形式提供。它们只需确保对于整数类型的实参 num std :: log10 ( num ) 能够产生与 std :: log10 ( static_cast < double > ( num ) ) 相同的效果。

示例

#include <cerrno>
#include <cfenv>
#include <cmath>
#include <cstring>
#include <iostream>
// #pragma STDC FENV_ACCESS ON
int main()
{
    std::cout << "log10(1000) = " << std::log10(1000) << '\n'
              << "log10(0.001) = " << std::log10(0.001) << '\n'
              << "base-5 logarithm of 125 = "
              << std::log10(125) / std::log10(5) << '\n';
    // 特殊值
    std::cout << "log10(1) = " << std::log10(1) << '\n'
              << "log10(+Inf) = " << std::log10(INFINITY) << '\n';
    // 错误处理
    errno = 0;
    std::feclearexcept(FE_ALL_EXCEPT);
    std::cout << "log10(0) = " << std::log10(0) << '\n';
    if (errno == ERANGE)
        std::cout << "    errno == ERANGE: " << std::strerror(errno) << '\n';
    if (std::fetestexcept(FE_DIVBYZERO))
        std::cout << "    FE_DIVBYZERO raised\n";
}

可能的输出:

log10(1000) = 3
log10(0.001) = -3
base-5 logarithm of 125 = 3
log10(1) = 0
log10(+Inf) = inf
log10(0) = -inf
    errno == ERANGE: Numerical result out of range
    FE_DIVBYZERO raised

参见

(C++11) (C++11)
计算自然(以 e 为底)对数( ln(x)
(函数)
(C++11) (C++11) (C++11)
计算以 2 为底的对数( log 2 (x)
(函数)
(C++11) (C++11) (C++11)
计算1加给定数值的自然对数(以 e 为底)( ln(1+x)
(函数)
沿负实轴有分支切割的复常用对数
(函数模板)
对valarray的每个元素应用函数 std::log10
(函数模板)
C 文档 for log10