frexp(value_type x,int* exp)函数详解

首页 / C++入门教程 / frexp(value_type x,int* exp)函数详解

此函数将浮点数分解为二进制有效数字和整数指数。

设浮点数为x,

x = (significand)*2e

其中," e"是指数," significand"是二进制有效数

frexp - 语法

假设浮点数为" 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);

frexp - 参数

x      :要分解为二进制有效位数的值。

exp :它是一个指向int的指针,该值存储指数值。

frexp - 返回值

它返回二进制有效位,其绝对值在0.5(包括)和1(排除)之间。

参数 Significand exponent
x=0 zero zero
x>=1 positive number positive number
x>= -1 negative number positive number
-1<x<0 negative number negative number
0<x<1 positive number negative number

frexp - 例子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

在此示例中,当x的值大于1时,frexp()函数将计算浮点数的二进制有效位数。

frexp - 例子2

让我们看一个简单的例子,当x的值为零时

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-math-frexp-function.html

来源:LearnFk无涯教程网

#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()函数计算浮点数的二进制有效位数。

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()函数将计算浮点数的二进制有效位数。

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()函数计算浮点数的二进制有效位数。

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的二进制有效位数。

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

赵成的运维体系管理课 -〔赵成〕

持续交付36讲 -〔王潇俊〕

Android开发高手课 -〔张绍文〕

编译原理之美 -〔宫文学〕

正则表达式入门课 -〔涂伟忠〕

OAuth 2.0实战课 -〔王新栋〕

Flink核心技术与实战 -〔张利兵〕

手把手带你搭建秒杀系统 -〔佘志东〕

编程高手必学的内存知识 -〔海纳〕

好记忆不如烂笔头。留下您的足迹吧 :)