std::log, std::logf, std::logl
來自 cppreference.com
定義於標頭檔案 <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
|