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

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

来源:LearnFk无涯教程网

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

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

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

技术教程推荐

移动端自动化测试实战 -〔思寒〕

小马哥讲Spring核心编程思想 -〔小马哥〕

分布式协议与算法实战 -〔韩健〕

操作系统实战45讲 -〔彭东〕

程序员的测试课 -〔郑晔〕

郭东白的架构课 -〔郭东白〕

Web漏洞挖掘实战 -〔王昊天〕

网络排查案例课 -〔杨胜辉〕

Rust 语言从入门到实战 -〔唐刚〕

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