- cmath[meta header]
- std[meta namespace]
- function[meta id-type]
- [mathjax enable]
- cpp11[meta cpp]
namespace std {
float exp2(float x);
double exp2(double x);
long double exp2(long double x);
double exp2(Integral x); // C++11 から
float exp2f(float x); // C++17 から
long double exp2l(long double x); // C++17 から
}- Integral[italic]
2 を底とする指数関数を求める。
2 の x 乗を返す。
x の絶対値が大きすぎる場合には、オーバーフローエラー、あるいはアンダーフローエラーとなり、戻り値は処理系定義である。(備考参照)
- $$ f(x) = 2^x $$
- オーバーフローエラー、アンダーフローエラーが発生した場合の挙動については、
<cmath>を参照。 - 処理系が IEC 60559 に準拠している場合(
std::numeric_limits<T>::is_iec559() != false)、以下の規定が追加される。x = ±0の場合、戻り値は1となる。x = -∞の場合、戻り値は+0となる。x = +∞の場合、戻り値は+∞となる。
#include <cmath>
#include <limits>
#include <iostream>
int main() {
std::cout << std::fixed;
std::cout << "exp2(0.0) = " << std::exp2(0.0) << std::endl;
std::cout << "exp2(1.0) = " << std::exp2(1.0) << std::endl;
std::cout << "exp2(3.0) = " << std::exp2(3.0) << std::endl;
std::cout << "exp2(4.0) = " << std::exp2(4.0) << std::endl;
std::cout << "exp2(+∞) = " << std::exp2(std::numeric_limits<double>::infinity()) << std::endl;
std::cout << "exp2(-∞) = " << std::exp2(-std::numeric_limits<double>::infinity()) << std::endl;
}- std::exp2[color ff0000]
- std::fixed[link ../ios/fixed.md]
- infinity[link ../limits/numeric_limits/infinity.md]
exp2(0.0) = 1.000000
exp2(1.0) = 2.000000
exp2(3.0) = 8.000000
exp2(4.0) = 16.000000
exp2(+∞) = inf
exp2(-∞) = 0.000000
namespace std {
double exp2(double x) {
return std::pow(2.0, x);
}
float exp2(float x) {
return std::pow(2.0f, x);
}
long double exp2(long double x) {
return std::pow(2.0L, x);
}
}- std::pow[link pow.md]
- C++11
- Clang: 2.9, 3.1
- GCC, C++11 mode: 4.3.4, 4.4.5, 4.5.2, 4.6.1, 4.7.0
特定の環境で constexpr 指定されている場合がある。(独自拡張)
- GCC 4.6.1 以上
exp があれば、以下のように変換することで求められる。