std::ldexp, std::ldexpf, std::ldexpl
From cppreference.com
| ヘッダー <cmath> で定義 |
||
| (1) | ||
float ldexp ( float num, int exp ); double ldexp ( double num, int exp ); |
(C++23まで) | |
| constexpr /* floating-point-type */ ldexp ( /* 浮動小数点型 */ 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) |
| 追加のオーバーロード (C++11以降) |
||
| ヘッダー <cmath> で定義 |
||
| template< class Integer > double ldexp ( 整数型 num, int exp ); |
(A) | (C++11以降) (C++23 以降 constexpr) |
1-3) 浮動小数点値 num に、2 の exp 乗を掛けます。C++23 以降、ライブラリは、パラメータ num の型として、すべての cv 修飾されていない浮動小数点型に対する
std::ldexp のオーバーロードを提供します。(C++23 以降)|
A) すべての整数型に対する追加のオーバーロードが提供されます。これらは double として扱われます。
|
(C++11以降) |
目次 |
[編集] パラメータ
| num | - | 浮動小数点数または整数値 |
| exp | - | 整数値 |
[編集] 戻り値
エラーが発生しない場合、num に 2 の exp 乗を掛けた値 (num×2exp
) を返します。
オーバーフローによる範囲エラーが発生した場合、±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(「load exponent」)は、その双対である 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) << " (minimum positive subnormal float64_t)\n" << "ldexp(nextafter(1,0), 1024) = " << std::ldexp(std::nextafter(1,0), 1024) << " (largest finite float64_t)\n"; // special values std::cout << "ldexp(-0, 10) = " << std::ldexp(-0.0, 10) << '\n' << "ldexp(-Inf, -1) = " << std::ldexp(-INFINITY, -1) << '\n'; // error handling 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 raised\n"; }
実行結果の例
ldexp(5, 3) = 5 * 8 = 40
ldexp(7, -4) = 7 / 16 = 0.4375
ldexp(1, -1074) = 4.94066e-324 (minimum positive subnormal float64_t)
ldexp(nextafter(1,0), 1024) = 1.79769e+308 (largest finite float64_t)
ldexp(-0, 10) = -0
ldexp(-Inf, -1) = -inf
ldexp(1, 1024) = inf
errno == ERANGE: Numerical result out of range
FE_OVERFLOW raised[編集] 関連項目
| (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 を返す (2x) (関数) |
| C のドキュメント (ldexp)
| |