C++ Math frexp
This function breaks the floating point number into the binary significand and an integral exponent.
Let the floating point number be x then,
where , 'e' is the exponent and 'significand' is the binary significand
Syntax
Suppose a floating point number be 'x' and pointer be '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);
Parameter
x : The value which is to be decomposed into the binary significand.
exp : It is a pointer to an int, where the value of exponent is stored.
Return value
It returns the binary significand which is the absolute value lies between 0.5(included) and 1(excluded).
| Parameter | 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 |
Example 1
Let's see a simple example when the value of x is greater than 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;
}
Output:
Value of x is : 2
2=0.5 * 2^2
In this example, frexp function calculates the binary significand of a floating point number when the value of x is greater than 1.
Example 2
Let's see a simple example when the value of x is zero
#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;
}
Output:
Value of x is : 0
0=0 * 2^0
In this example, frexp function calculates the binary significand of a floating point number when the value of x is zero.
Example 3
Let's see a simple example when the value of x lies between 0 and 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;
}
Output:
Value of x is : 0.4
0.4=0.8 * 2^-1
In this example, frexp function calculates the binary significand of a floating point number when the value of x lies between 0 and 1.
Example 4
Let's see a simple example when the value of x lies between -1 and 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;
}
Output:
Value of x is : -0.1
-0.1=-0.8 * 2^-3
In this example, frexp function calculates the binary significand of a floating point number when the value of x lies between -1 and 0.
Example 5
Let's see the simple example when the value of x is less than -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;
}
Output:
Value of x is : -5
-5=-0.625 * 2^3
In this example, frexp function calculates the binary significand of a floating point nmber when the value of x is less than -1.