C++ Math frexp()
C++ Math frexp()
此函数将浮点数分解为二进制有效数和整数指数。
让浮点数为x然后,
其中," e"是指数," 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的指针,在其中存储了指数值。
返回值
它返回二进制有效值,即绝对值值介于0.5(含)和1(不含)之间。
参数 |
有效位数 |
指数 |
x = 0 |
零 |
零 |
x> = 1 |
正数 |
正数 |
x> =-1 |
负数 |
正数 |
-1
|
负数 |
负数 |
0
|
正数 |
负数 |
示例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()函数将计算浮点数的二进制有效数字。
示例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的二进制有效数字。