In C++, the frexp()
function is used to return the multiple of the binary significand and 2
raised to the power of the exponent. In simpler words, the frexp()
function returns the binary significand value of a floating point number.
Mathematically:
frexp(x, exp) => x = binary significand *
Note: A floating point number whose absolute value lies between
0.5
and1
is referred to as the binary significand.
double frexp (double x, int* exp);
float frexp (float x, int* exp);
long double frexp (long double x, int* exp);
The frexp()
function takes two parameter values:
x
: This represents the value that needs to be decomposed.exp
: This represents the pointer to an integer where the exponent’s value is stored.The frexp()
function returns the binary signinificand value of type double
, float
, or long double
. These absolute values lie between 0.5
and 1
. If the given x
parameter is 0
, then both the significand and the exponent values are 0
.
#include <iostream>#include <cmath>using namespace std;int main (){// creatimg our variablesdouble x = 7.24, significand;int *exp;// using the frexp() functionsignificand = frexp(x , exp);cout << x << " = " << significand << " * 2^" << *exp << endl;return 0;}
Lines 9–10: We create our variables x
, significand
, and *exp
.
Line 13: We implement the frexp()
function, using x
and exp
as the parameter values of the function. We assign the output of the result to the variable signifciand
.
Line 14: We print the variable x
alongside the variables significand
and exp
.