名前空間
変種
操作

std::frexp, std::frexpf, std::frexpl

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

double      frexp ( double num, int* exp );

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

目次

[編集] パラメータ

num - 浮動小数点数または整数値
exp - 指数を格納するための整数値へのポインタ

[編集] 戻り値

num がゼロの場合、ゼロを返し、*exp にゼロを格納します。

それ以外の場合(num がゼロでない場合)、エラーが発生しなければ、範囲 (-1, -0.5], [0.5, 1) の値 x を返し、*exp に整数値を格納します。ただし、x×2(*exp)== num となります。

*exp に格納される値が int の範囲外である場合、動作は未定義です。

[編集] エラー処理

この関数は、math_errhandlingで指定されているエラーの対象とはならない。

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

  • num が ±0 の場合、変更されずに返され、0*exp に格納されます。
  • num が ±∞ の場合、返され、*exp には未定義の値が格納されます。
  • num が NaN の場合、NaN が返され、*exp には未定義の値が格納されます。
  • 浮動小数点例外は発生しません。
  • FLT_RADIX が 2(または2のべき乗)の場合、返される値は正確であり、現在の丸めモードは無視されます。

[編集]

2進システム(FLT_RADIX2 の場合)では、std::frexp は次のように実装される場合があります。

{
    *exp = (value == 0) ? 0 : (int)(1 + std::logb(value));
    return std::scalbn(value, -(*exp));
}

関数 std::frexp は、その双対である std::ldexp とともに、ビット演算を直接行わずに浮動小数点数の表現を操作するために使用できます。

追加のオーバーロードは、(A) のように正確に提供される必要はありません。整数型の引数 num に対して、std::frexp(num, exp)std::frexp(static_cast<double>(num), exp) と同じ効果を持つことを保証するだけで十分です。

[編集]

異なる浮動小数点分解関数を比較します。

#include <cmath>
#include <iostream>
#include <limits>
 
int main()
{
    double f = 123.45;
    std::cout << "Given the number " << f << " or " << std::hexfloat
              << f << std::defaultfloat << " in hex,\n";
 
    double f3;
    double f2 = std::modf(f, &f3);
    std::cout << "modf() makes " << f3 << " + " << f2 << '\n';
 
    int i;
    f2 = std::frexp(f, &i);
    std::cout << "frexp() makes " << f2 << " * 2^" << i << '\n';
 
    i = std::ilogb(f);
    std::cout << "logb()/ilogb() make " << f / std::scalbn(1.0, i)
              << " * " << std::numeric_limits<double>::radix
              << "^" << std::ilogb(f) << '\n';
}

実行結果の例

Given the number 123.45 or 0x1.edccccccccccdp+6 in hex,
modf() makes 123 + 0.45
frexp() makes 0.964453 * 2^7
logb()/ilogb() make 1.92891 * 2^6

[編集] 関連項目

(C++11)(C++11)
数値に2の整数乗を掛ける
(関数) [編集]
(C++11)(C++11)(C++11)
数値の指数部を抽出する
(関数) [編集]
(C++11)(C++11)(C++11)
数値の指数部を抽出する
(関数) [編集]
(C++11)(C++11)
数値を整数部と小数部に分解する
(関数) [編集]
English 日本語 中文(简体) 中文(繁體)