此函数将浮点数分解为二进制有效数字和整数指数。
设浮点数为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(排除)之间。
参数 | 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 |
让我们看一个简单的示例,当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无涯教程网
让我们看一个简单的例子,当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()函数计算浮点数的二进制有效位数。
让我们看一个简单的示例,其中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()函数将计算浮点数的二进制有效位数。
让我们看一个简单的示例,其中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()函数计算浮点数的二进制有效位数。
让我们看一个简单的例子,当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的二进制有效位数。
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)