std::log, std::logf, std::logl
来自 cppreference.cn
定义于头文件 <cmath> |
||
(1) | ||
float log ( float num ); double log ( double num ); |
(直至 C++23) | |
/*浮点类型*/ log ( /*浮点类型*/ num ); |
(C++23 起) (C++26 起为 constexpr) |
|
float logf( float num ); |
(2) | (C++11 起) (C++26 起为 constexpr) |
long double logl( 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 log ( Integer num ); |
(A) | (C++26 起为 constexpr) |
S) SIMD 重载对 v_num 的每个元素执行
std::log 。
|
(C++26 起) |
A) 为所有整数类型提供了额外的重载,它们被视为 double。
|
(C++11 起) |
目录 |
[编辑] 参数
num | - | 浮点值或整数值 |
[编辑] 返回值
如果没有错误发生,则返回 num 的自然(底数 e)对数(ln(num) 或 loge(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::log(num) 具有与 std::log(static_cast<double>(num)) 相同的效果。
[编辑] 示例
运行此代码
#include <cerrno> #include <cfenv> #include <cmath> #include <cstring> #include <iostream> // #pragma STDC FENV_ACCESS ON int main() { std::cout << "log(1) = " << std::log(1) << '\n' << "base-5 logarithm of 125 = " << std::log(125) / std::log(5) << '\n'; // special values std::cout << "log(1) = " << std::log(1) << '\n' << "log(+Inf) = " << std::log(INFINITY) << '\n'; // error handling errno = 0; std::feclearexcept(FE_ALL_EXCEPT); std::cout << "log(0) = " << std::log(0) << '\n'; if (errno == ERANGE) std::cout << " errno == ERANGE: " << std::strerror(errno) << '\n'; if (std::fetestexcept(FE_DIVBYZERO)) std::cout << " FE_DIVBYZERO raised\n"; }
可能的输出
log(1) = 0 base-5 logarithm of 125 = 3 log(1) = 0 log(+Inf) = inf log(0) = -inf errno == ERANGE: Numerical result out of range FE_DIVBYZERO raised
[编辑] 参阅
(C++11)(C++11) |
计算常用(底数 10)对数(log10(x)) (函数) |
(C++11)(C++11)(C++11) |
给定数字的底数 2 对数(log2(x)) (函数) |
(C++11)(C++11)(C++11) |
给定数字加 1 的自然对数(底数 e)(ln(1+x)) (函数) |
(C++11)(C++11) |
返回 e 的给定幂(ex) (函数) |
具有沿负实轴分支割线的复数自然对数 (函数模板) | |
将函数 std::log 应用于 valarray 的每个元素 (函数模板) | |
C 文档 用于 log
|