C++ cmath frexp,frexpf and frexpl function


The C++ <cmath> frexp,frexpf and frexpl function breaks the floating-point number into normalized fraction and integral power of 2.The declaration of the functions are given below.

1 float frexp(float x, int* exp);
2 double frexp(double x, int* exp);
3 long double frexp(long double x , int* exp);
4 float frexpf(float x , int* exp);
5 long double frexpl(long double x , int* exp);

Parameters:
x – A floating point value to be partitioned into fractional value and integral power of 2.

*exp -This parameter stores the value which is the integral power of 2.

Return type
floating point -The fractional value obtained after partitioning the x.

Some points to note:

i) Suppose frexp(x , *exp), returns ‘value‘ , than x=value * 2exp.

ii) If ‘x’ is not a floating-point value ,the result is unspecified.

iii) If 0 is passed the function returns 0 and exp is also 0.

iv) If NAN is passed ‘nan’ is returned and ‘exp’ value is compiler dependent.In MinGW ‘exp’ value is some garbage value and VS gives -1.

v) If INFINITY is passed ‘inf’ is returned and ‘exp’ value is compiler dependent.In MinGW ‘exp’ is some garbage value and VS gives -1.

vi) If int or character is passed the default double version-the 2nd version- is called.

vii) If string is passed as the first argument you get an error.

Code example

/*Passing floating point value*/
int exp ;
double value=frexp(23 , &exp) ;
cout<< “value=” << value endl
<< “exp=” << exp << endl ;

/*Passing 0*/
int exp1;
cout<< frexp(0 , &exp1 ) ;
cout<< exp1 ;

/*Passing NAN */
cout<< frexp( NAN , &exp1 ) ;

/*Passing INFINITY*/
cout<< frexp(INFINITY , &exp1 ) ;

Output

value= 0.546875
exp= 5
0
0
nan
inf

To verify the ‘value’ and the ‘exp’ value of ‘x’ look at the code below.

cout<< value* pow(2,exp) ; //the ‘value’ and ‘exp’ value is taken from the above program

Output

23


 


frexpf and frexpl

*Note

i)The 4th version frexpf is same as the 1st version float frexp(float).The ‘f‘ character stands for ‘float’ which signify the argument and return type of the function.

ii)The 5th version frexpl is same as the 3rd version long double frexp(long double).The ‘l‘ character stands for ‘long double’ which signify the argument and return type of the function.

Code example

#include <typeinfo>

/*frexpf */
cout<< “*frexpf\n”;
float f=2;
int exp1;
cout<< typeid( frexpf(2 , &exp1) ).name() << endl /*identify type of frexpf retunred value */
<< typeid( frexp(f , &exp1) ).name() << endl; /*identify type of frexp(float) returned value */

/*frexpl*/
cout<< “*frexpl\n”;
long double ld=2;
cout<< typeid( frexpl(2 , &exp1) ).name() << endl /*indentify type of frexpl returned value */
<< typeid( frexp(ld , &exp1) ).name() << endl; /*identify type of frexp(long double) returned value*/

Output

*frexpf
f
f
*frexpl
e (means ‘long double’)
e (means ‘long double’)