std::exp2, std::exp2f, std::exp2l
來自 cppreference.com
定義於標頭檔案 <cmath> |
||
(1) | ||
float exp2 ( float num ); double exp2 ( double num ); |
(直至 C++23) | |
/*浮點型別*/ exp2 ( /*浮點型別*/ num ); |
(C++23 起) (C++26 起為 constexpr) |
|
float exp2f( float num ); |
(2) | (C++11 起) (C++26 起為 constexpr) |
long double exp2l( 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 exp2 ( Integer num ); |
(A) | (C++26 起為 constexpr) |
1-3) 計算 2 的給定冪次 num。 庫為所有 cv 非限定浮點型別提供了
std::exp2
的過載作為引數的型別。(C++23 起)
S) SIMD 過載對 v_num 執行逐元素的
std::exp2 。
|
(C++26 起) |
A) 為所有整數型別提供了額外的過載,它們被視為 double。
|
(C++11 起) |
目錄 |
[編輯] 引數
num | - | 浮點值或整數值 |
[編輯] 返回值
如果無錯誤發生,則返回 num 的以 2 為底的指數 (2num
)。
如果發生因溢位導致的範圍錯誤,返回 +HUGE_VAL、+HUGE_VALF
或 +HUGE_VALL
。
如果因下溢發生範圍錯誤,則返回正確結果(舍入後)。
[編輯] 錯誤處理
錯誤按 math_errhandling 指定的方式報告。
如果實現支援 IEEE 浮點運算 (IEC 60559),
- 若引數為 ±0,則返回 1。
- 若引數為 -∞,則返回 +0。
- 如果引數是 +∞,則返回 +∞。
- 如果引數為 NaN,則返回 NaN。
[編輯] 註記
無需完全按 (A) 提供額外的過載。它們只需足以確保對於整數型別的引數 num,std::exp2(num) 具有與 std::exp2(static_cast<double>(num)) 相同的效果。
對於整數指數,使用 std::ldexp 可能更可取。
[編輯] 示例
執行此程式碼
#include <cerrno> #include <cfenv> #include <cmath> #include <cstring> #include <iostream> // #pragma STDC FENV_ACCESS ON int main() { std::cout << "exp2(4) = " << std::exp2(4) << '\n' << "exp2(0.5) = " << std::exp2(0.5) << '\n' << "exp2(-4) = " << std::exp2(-4) << '\n'; // special values std::cout << "exp2(-0) = " << std::exp2(-0.0) << '\n' << "exp2(-Inf) = " << std::exp2(-INFINITY) << '\n'; // error handling errno = 0; std::feclearexcept(FE_ALL_EXCEPT); const double inf = std::exp2(1024); const bool is_range_error = errno == ERANGE; std::cout << "exp2(1024) = " << inf << '\n'; if (is_range_error) std::cout << " errno == ERANGE: " << std::strerror(ERANGE) << '\n'; if (std::fetestexcept(FE_OVERFLOW)) std::cout << " FE_OVERFLOW raised\n"; }
可能的輸出
exp2(4) = 16 exp2(0.5) = 1.41421 exp2(-4) = 0.0625 exp2(-0) = 1 exp2(-Inf) = 0 exp2(1024) = inf errno == ERANGE: Numerical result out of range FE_OVERFLOW raised
[編輯] 參閱
(C++11 起)(C++11 起) |
返回 e 的給定冪(ex) (函式) |
(C++11 起)(C++11 起)(C++11 起) |
返回 e 的給定冪次減 1 (ex-1) (函式) |
(C++11 起)(C++11 起) |
將數字乘以 2 的整數次冪 (函式) |
(C++11 起)(C++11 起)(C++11 起) |
給定數的以 2 為底的對數 (log2(x)) (函式) |
C 文件 關於 exp2
|