C++ Math frexp
This function separates the floating-point number into the binary significand and an integer exponent.
Let the floating point number be x then,
where 'e' represents the exponent and 'significand' stands for the binary significand
Syntax
Consider a floating-point value denoted as 'x' and a pointer indicated as '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 that needs to be broken down into the binary significand.
It represents a pointer to an integer that holds the value of the exponent.
Return value
It returns the binary significand, an absolute value that falls within the range of 0.5 (inclusive) and less than 1.
| 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 consider a basic scenario where the value of x exceeds 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 instance, the frexp function computes the binary mantissa of a floating-point value if x exceeds 1.
Example 2
Let's examine a basic scenario where the value of x equals 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 instance, the frexp function computes the binary mantissa of a floating-point number if the input x equals zero.
Example 3
Let's examine a basic scenario where the value of x falls within the range of 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 instance, the frexp function computes the binary mantissa of a floating-point value when x falls within the range of 0 and 1.
Example 4
Let's consider a straightforward scenario where the value of x falls within the range of -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 instance, the frexp function determines the binary fraction part of a floating-point value for input x within the range of -1 and 0.
Example 5
Let's examine a basic scenario where the variable x is below -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 instance, the frexp function computes the binary mantissa of a floating-point number if the input x is smaller than -1.