std::exp, std::expf, std::expl
來自 cppreference.com
定義於標頭檔案 <cmath> |
||
(1) | ||
float exp ( float num ); double exp ( double num ); |
(直至 C++23) | |
/*浮點型別*/ exp ( /*浮點型別*/ num ); |
(C++23 起) (C++26 起為 constexpr) |
|
float expf( float num ); |
(2) | (C++11 起) (C++26 起為 constexpr) |
long double expl( 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 exp ( Integer num ); |
(A) | (C++26 起為 constexpr) |
S) SIMD 過載對 v_num 執行逐元素的
std::exp 。
|
(C++26 起) |
A) 為所有整數型別提供了額外的過載,它們被視為 double。
|
(C++11 起) |
目錄 |
[編輯] 引數
num | - | 浮點值或整數值 |
[編輯] 返回值
如果沒有發生錯誤,返回 num 的自然指數(enum
)。
如果由於溢位導致範圍錯誤,則返回 +HUGE_VAL、+HUGE_VALF
或 +HUGE_VALL
。
如果因下溢發生範圍錯誤,則返回正確結果(舍入後)。
[編輯] 錯誤處理
錯誤按 math_errhandling 指定的方式報告。
如果實現支援 IEEE 浮點運算 (IEC 60559),
- 如果引數為 ±0,則返回 1。
- 如果引數為 -∞,則返回 +0。
- 如果引數是 +∞,則返回 +∞。
- 如果引數為 NaN,則返回 NaN。
[編輯] 注意
對於符合 IEEE 標準的 double 型別,當 709.8 < num 時保證溢位,當 num < -708.4 時保證下溢。
不要求完全按照 (A) 提供額外的過載。它們只需要足以確保對於整數型別的引數 num,std::exp(num) 具有與 std::exp(static_cast<double>(num)) 相同的效果。
[編輯] 示例
執行此程式碼
#include <cerrno> #include <cfenv> #include <cmath> #include <cstring> #include <iomanip> #include <iostream> #include <numbers> // #pragma STDC FENV_ACCESS ON consteval double approx_e() { long double e{1.0}; for (auto fac{1ull}, n{1llu}; n != 18; ++n, fac *= n) e += 1.0 / fac; return e; } int main() { std::cout << std::setprecision(16) << "exp(1) = e¹ = " << std::exp(1) << '\n' << "numbers::e = " << std::numbers::e << '\n' << "approx_e = " << approx_e() << '\n' << "FV of $100, continuously compounded at 3% for 1 year = " << std::setprecision(6) << 100 * std::exp(0.03) << '\n'; // special values std::cout << "exp(-0) = " << std::exp(-0.0) << '\n' << "exp(-Inf) = " << std::exp(-INFINITY) << '\n'; // error handling errno = 0; std::feclearexcept(FE_ALL_EXCEPT); std::cout << "exp(710) = " << std::exp(710) << '\n'; if (errno == ERANGE) std::cout << " errno == ERANGE: " << std::strerror(errno) << '\n'; if (std::fetestexcept(FE_OVERFLOW)) std::cout << " FE_OVERFLOW raised\n"; }
可能的輸出
exp(1) = e¹ = 2.718281828459045 numbers::e = 2.718281828459045 approx_e = 2.718281828459045 FV of $100, continuously compounded at 3% for 1 year = 103.045 exp(-0) = 1 exp(-Inf) = 0 exp(710) = inf errno == ERANGE: Numerical result out of range FE_OVERFLOW raised
[編輯] 另見
(C++11)(C++11)(C++11) |
返回 2 的給定冪(2x) (函式) |
(C++11)(C++11)(C++11) |
返回 e 的給定冪減去 1(ex-1) (函式) |
(C++11)(C++11) |
計算自然(底數為 e)對數(ln(x)) (函式) |
複數 e 的指數 (函式模板) | |
將函式 std::exp 應用於 valarray 的每個元素 (函式模板) | |
C 文件 用於 exp
|