std::log10, std::log10f, std::log10l
来自 cppreference.cn
定义于头文件 <cmath> |
||
(1) | ||
float log10 ( float num ); double log10 ( 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< /*数学浮点类型*/ V > constexpr /*推导 SIMD 类型*/<V> |
(S) | (C++26 起) |
额外重载 (自 C++11 起) |
||
定义于头文件 <cmath> |
||
template< class Integer > double log10 ( Integer num ); |
(A) | (C++26 起为 constexpr) |
S) SIMD 重载对 v_num 执行元素级别的
std::log10 操作。
|
(C++26 起) |
A) 为所有整数类型提供了额外的重载,它们被视为 double。
|
(C++11 起) |
目录 |
[编辑] 参数
num | - | 浮点值或整数值 |
[编辑] 返回值
如果没有错误发生,则返回 num 的常用(以 10 为底)对数(log10(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'; // special values std::cout << "log10(1) = " << std::log10(1) << '\n' << "log10(+Inf) = " << std::log10(INFINITY) << '\n'; // error handling 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 为底的对数 (log2(x)) (函数) |
(C++11)(C++11)(C++11) |
1 加上给定数字的自然对数(以 e 为底)(ln(1+x)) (函数) |
具有沿负实轴分支割线的复数常用对数 (函数模板) | |
将函数 std::log10 应用到 valarray 的每个元素 (函数模板) | |
C 文档 关于 log10
|