FRXP()在C++中的应用

函数的作用是:将浮点数x分解为二进制有效位,即绝对值在[0.5,1.0]和整数指数2之间的浮点。

null

x=有效位*(2^指数)。 它曾经:

1. It is used to find significand which is always between 0.5 and 1.0
2. It is used to find exponent which is power of 2.

语法:

double frexp (double x);
float frexp (float x);
long double frexp (long double x);

    参数:

  • 函数接受一个参数。

    返回:

  • 函数的作用是:返回绝对值在区间[0.5,1]内的二进制有效位。
  • 如果x为零,则有效位和指数均为零。

错误和异常:

  1. 必须同时给出两个参数,否则会出现错误 调用“frexp()”时没有匹配的函数 .
  2. 如果我们传递一个字符串作为参数,我们将得到一个错误:调用’frexp(const char[n])时没有匹配的函数。
    • 如果x=0,则有效位为零,指数为零
    • x>=1则有效位为正数,指数为正数
    • x<=-1则有效位为负数,指数为正数
    • -1
    • 0

#代码1

// CPP implementation of
// above function
#include <cmath>
#include <iostream>
using namespace std;
// Driver program
int main()
{
double x = 5.35, significand;
int exponent;
significand = frexp (x, &exponent);
cout << x << " = " << significand
<< " * 2^" << exponent << endl;
return 0;
}


输出:

5.35 = 0.66875 * 2^3

#代码2

// CPP implementation of the
// above function
#include <cmath>
#include <iostream>
using namespace std;
// Driver program
int main()
{
double significand;
int exponent, x = 5;
significand = frexp (x, &exponent);
cout << x << " = " << significand
<< " * 2^" << exponent << endl;
return 0;
}


输出:

5 = 0.625 * 2^3

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享