名稱空間
變體
操作

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

來自 cppreference.com
< cpp‎ | numeric‎ | math
 
 
 
常用數學函式
函式
基本操作
(C++11)  
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
指數函式
(C++11)
(C++11)

(C++11)
(C++11)
冪函式
(C++11)
(C++11)
三角
雙曲函式
(C++11)
(C++11)
(C++11)

誤差函式和伽馬函式
(C++11)
(C++11)
(C++11)
(C++11)
取整浮點運算
(C++11)(C++11)(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
浮點操縱函式
(C++11)(C++11)
(C++11)
(C++11)
frexp
(C++11)(C++11)
(C++11)
分類和比較
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
型別
(C++11)
(C++11)
(C++11)
宏常量
分類
(C++11)(C++11)(C++11)(C++11)(C++11)


 
定義於標頭檔案 <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 ( /* floating-point-type */ 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)
額外過載 (自 C++11 起)
定義於標頭檔案 <cmath>
template< class Integer >
double      frexp ( Integer num, int* exp );
(A) (自 C++23 起為 constexpr)
1-3) 將給定的浮點值 num 分解為規範化小數和 2 的整數指數。 庫為所有 cv-不限定的浮點型別(作為引數 num 的型別)提供了 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 的冪),則返回的值是精確的,當前的舍入模式被忽略。

[編輯] 注意

在二進位制系統上(其中 FLT_RADIX2),std::frexp 可以實現為

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

函式 std::frexp 及其對偶 std::ldexp 可以用於操作浮點數的表示而無需直接位操作。

不需要完全按照 (A) 提供額外的過載。它們只需要足以確保對於整數型別的引數 numstd::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)
將數字分解為整數部分和小數部分
(函式) [編輯]
C 文件frexp