當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C++ Math frexp()用法及代碼示例

此函數將浮點數分解為二進製有效數和整數 index 。

令浮點數為 x 然後,

x =(有效數)*2e

其中,'e' 是 index ,'significand' 是二進製有效數

用法

假設浮點數為 'x',指針為 'exp':

float frexp(float x, int* exp);
double frexp(double x, int* exp);
long double frexp(long double x, int* exp);
double frexp(integral x, int* exp);

參數

x: 要分解為二進製有效數的值。

exp: 它是一個指向 int 的指針,其中存儲了 index 的值。

返回值

它返回絕對值介於 0.5(包括)和 1(不包括)之間的二進製有效數。

參數 有效數 index
x=0 zero zero
x>=1 正數 正數
x>= -1 負數 正數
-1<x<0 負數 負數
0<x<1 正數 負數

例子1

我們來看一個簡單的例子,當 x 的值大於 1 時。

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
    double x=2;
    int* e;
    cout<<"Value of x is:"<<x<<'\n';
    double significand = frexp(x,e);
    std::cout <<x<<"="<<significand<<" * "<<"2^"<<*e;
    return 0;
}

輸出:

Value of x is:2
2=0.5 * 2^2

在本例中,frexp() 函數在 x 的值大於 1 時計算浮點數的二進製有效數。

例子2

讓我們看一個簡單的例子,當 x 的值為零時

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
    double x=0;
    int* e;
    cout<<"Value of x is:"<<x<<'\n';
    double significand = frexp(x,e);
    std::cout <<x<<"="<<significand<<" * "<<"2^"<<*e;
    return 0;
}

輸出:

Value of x is:0
0=0 * 2^0

在本例中,當 x 的值為零時,frexp() 函數計算浮點數的二進製有效數。

例子3

讓我們看一個簡單的例子,當 x 的值介於 0 和 1 之間時。

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
    double x=0.4;
    int* e;
    cout<<"Value of x is:"<<x<<'\n';
    double significand = frexp(x,e);
    std::cout <<x<<"="<<significand<<" * "<<"2^"<<*e;
    return 0;
}

輸出:

Value of x is:0.4
0.4=0.8 * 2^-1

在本例中,當 x 的值介於 0 和 1 之間時,frexp() 函數計算浮點數的二進製有效數。

示例 4

讓我們看一個簡單的例子,當 x 的值介於 -1 和 0 之間時。

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
    double x= -0.1;
    int* e;
    cout<<"Value of x is:"<<x<<'\n';
    double significand = frexp(x,e);
    std::cout <<x<<"="<<significand<<" * "<<"2^"<<*e;
    return 0;
}

輸出:

Value of x is:-0.1
-0.1=-0.8 * 2^-3

在本例中,當 x 的值介於 -1 和 0 之間時,frexp() 函數計算浮點數的二進製有效數。

例 5

讓我們看一個簡單的例子,當 x 的值小於 -1 時。

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
    double x= -5;
    int* e;
    cout<<"Value of x is:"<<x<<'\n';
    double significand = frexp(x,e);
    std::cout <<x<<"="<<significand<<" * "<<"2^"<<*e;
    return 0;
}

輸出:

Value of x is:-5
-5=-0.625 * 2^3

在本例中,當 x 的值小於 -1 時,frexp() 函數計算浮點數 nmber 的二進製有效數。






相關用法


注:本文由純淨天空篩選整理自 C++ Math frexp()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。