C ++中的frexp(x,exp)函數(shù)返回一個(gè)浮點(diǎn)數(shù)的尾數(shù)和指數(shù)。
二進(jìn)制有效位數(shù)是一個(gè)浮點(diǎn),其絕對(duì)值(尾數(shù))在區(qū)間[0.5,1]中,整數(shù)指數(shù)為2。
該函數(shù)在<cmath>頭文件中定義。
數(shù)學(xué)上
x = Binary significand * 2exponent
其中,指數(shù)存儲(chǔ)在exp所指向的位置,而二進(jìn)制有效位數(shù)是frexp()返回的值。
double frexp (double x, int* exp); float frexp (float x, int* exp); long double frexp (long double x, int* exp); double frexp (T x, int* exp); //為整型
frexp()函數(shù)函數(shù)有兩個(gè)參數(shù),并返回double,float或long double類型的二進(jìn)制有效值。
x -要分解的值。
exp -指向要存儲(chǔ)指數(shù)值的整數(shù)的指針。
frexp()函數(shù)返回尾數(shù),其絕對(duì)值位于范圍[0.5,1]中。如果x為零,則有效數(shù)和指數(shù)均為零。
| 參數(shù)(x) | 二進(jìn)制有效位 | 指數(shù) |
|---|---|---|
| 0 | 0 | 0 |
| x> = 1 | 正 | 正 |
| x <= -1 | 負(fù) | 正 |
| -1 <x <0 | 負(fù) | 負(fù) |
| 0 <x <1 | 正 | 負(fù) |
#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
double x = 6.81, significand;
int *exp;
significand = frexp(x , exp);
cout << x << " = " << significand << " * 2^" << *exp << endl;
return 0;
}運(yùn)行該程序時(shí),輸出為:
6.81 = 0.85125 * 2^3
#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
double significand;
int *exp, x = 25;
significand = frexp (x , exp);
cout << x << " = " << significand << " * 2^" << *exp << endl;
return 0;
}運(yùn)行該程序時(shí),輸出為:
25 = 0.78125 * 2^5