名前空間
変種
操作

std::exp2, std::exp2f, std::exp2l

From cppreference.com
< cpp‎ | numeric‎ | math
 
 
 
 
ヘッダー <cmath> で定義
(1)
float       exp2 ( float num );

double      exp2 ( double num );

long double exp2 ( long 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> で定義
template< /*math-floating-point*/ V >

constexpr /*deduced-simd-t*/<V>

            exp2 ( const V& v_num );
(S) (C++26以降)
ヘッダー <cmath> で定義
template< class Integer >
double      exp2 ( Integer num );
(A) (C++26 以降 constexpr)
1-3) 指定されたべき乗 num2 を計算します。ライブラリは、パラメータの型としてすべてのcv修飾されていない浮動小数点数型に対する std::exp2 のオーバーロードを提供します。(C++23以降)
S) SIMDオーバーロードは、v_num に対して要素ごとの std::exp2 を実行します。
(定義については、math-floating-point および deduced-simd-t を参照してください。)
(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))
(関数) [編集]
English 日本語 中文(简体) 中文(繁體)