Namespaces
Variants

std:: ldexp, std:: ldexpf, std:: ldexpl

From cppreference.net
Common mathematical functions
Nearest integer floating point operations
(C++11)
(C++11)
(C++11) (C++11) (C++11)
Floating point manipulation functions
ldexp
(C++11) (C++11)
(C++11)
(C++11)
Classification and comparison
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Types
(C++11)
(C++11)
(C++11)
Macro constants
ヘッダーで定義 <cmath>
(1)
float ldexp ( float num, int exp ) ;

double ldexp ( double num, int exp ) ;

long double ldexp ( long double num, int exp ) ;
(C++23まで)
constexpr /* floating-point-type */
ldexp ( /* floating-point-type */ num, int exp ) ;
(C++23から)
float ldexpf ( float num, int exp ) ;
(2) (C++11から)
(C++23からconstexpr)
long double ldexpl ( long double num, int exp ) ;
(3) (C++11から)
(C++23からconstexpr)
ヘッダーで定義 <cmath>
template < class Integer >
double ldexp ( Integer num, int exp ) ;
(A) (C++11から)
(C++23からconstexpr)
1-3) 浮動小数点値 num 2 exp 乗で乗算します。 ライブラリは、 std::ldexp のオーバーロードを、パラメータ num の型としてすべてのcv修飾されていない浮動小数点型に対して提供します。 (C++23以降)
A) すべての整数型に対して追加のオーバーロードが提供されており、これらは double として扱われます。
(C++11以降)

目次

パラメータ

num - 浮動小数点または整数値
exp - 整数値

戻り値

エラーが発生しない場合、 num に2の exp 乗を掛けた値( num×2 exp
)が返されます。

オーバーフローによる範囲エラーが発生した場合、 ±HUGE_VAL ±HUGE_VALF または ±HUGE_VALL が返されます。

アンダーフローによる範囲エラーが発生した場合、正しい結果(丸め後)が返されます。

エラーハンドリング

エラーは math_errhandling で指定された通りに報告されます。

IEEE浮動小数点演算(IEC 60559)を実装がサポートしている場合、

  • 範囲エラーが発生しない限り、 FE_INEXACT は決して発生しません(結果は正確です)。
  • 範囲エラーが発生しない限り、 現在の丸めモード は無視されます。
  • num が±0の場合、変更されずに返されます。
  • num が±∞の場合、変更されずに返されます。
  • exp が0の場合、 num は変更されずに返されます。
  • num がNaNの場合、NaNが返されます。

注記

バイナリシステム( FLT_RADIX 2 の場合)では、 std::ldexp std::scalbn と等価です。

関数 std::ldexp (指数部のロード)とその双対である std::frexp は、浮動小数点数の表現を直接的なビット操作なしで操作するために使用できます。

多くの実装では、 std::ldexp は算術演算子を用いた2の累乗による乗算や除算よりも効率が劣ります。

追加のオーバーロードは (A) と完全に同一である必要はありません。整数型の引数 num に対して、 std :: ldexp ( num, exp ) std :: ldexp ( static_cast < double > ( num ) , exp ) と同じ効果を持つことを保証するのに十分なものであればよいのです。

浮動小数点指数による2の累乗については、 std::exp2 を使用できます。

#include <cerrno>
#include <cfenv>
#include <cmath>
#include <cstring>
#include <iostream>
// #pragma STDC FENV_ACCESS ON
int main()
{
    std::cout
        << "ldexp(5, 3) = 5 * 8 = " << std::ldexp(5, 3) << '\n'
        << "ldexp(7, -4) = 7 / 16 = " << std::ldexp(7, -4) << '\n'
        << "ldexp(1, -1074) = " << std::ldexp(1, -1074)
        << " (最小の正の非正規化数 float64_t)\n"
        << "ldexp(nextafter(1,0), 1024) = "
        << std::ldexp(std::nextafter(1,0), 1024)
        << " (最大の有限値 float64_t)\n";
    // 特殊な値
    std::cout << "ldexp(-0, 10) = " << std::ldexp(-0.0, 10) << '\n'
              << "ldexp(-Inf, -1) = " << std::ldexp(-INFINITY, -1) << '\n';
    // エラー処理
    std::feclearexcept(FE_ALL_EXCEPT);
    errno = 0;
    const double inf = std::ldexp(1, 1024);
    const bool is_range_error = errno == ERANGE;
    std::cout << "ldexp(1, 1024) = " << inf << '\n';
    if (is_range_error)
        std::cout << "    errno == ERANGE: " << std::strerror(ERANGE) << '\n';
    if (std::fetestexcept(FE_OVERFLOW))
        std::cout << "    FE_OVERFLOW が発生\n";
}

出力例:

ldexp(5, 3) = 5 * 8 = 40
ldexp(7, -4) = 7 / 16 = 0.4375
ldexp(1, -1074) = 4.94066e-324 (最小の正の非正規化数 float64_t)
ldexp(nextafter(1,0), 1024) = 1.79769e+308 (最大の有限値 float64_t)
ldexp(-0, 10) = -0
ldexp(-Inf, -1) = -inf
ldexp(1, 1024) = inf
    errno == ERANGE: 数値結果が範囲外です
    FE_OVERFLOW が発生

関連項目

(C++11) (C++11)
数値を仮数と基数 2 の指数に分解する
(関数)
(C++11) (C++11) (C++11) (C++11) (C++11) (C++11)
数値に FLT_RADIX のべき乗を乗算する
(関数)
(C++11) (C++11) (C++11)
与えられたべき乗の 2 を返す ( 2 x )
(関数)