C++ cmath frexp, frexpf, 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 integral power of 2.

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

The first function is called if the first argument if float type.

The second function is called if the first argument if double type.

The third function is called if the first argument if long double type.

The 4th and 5th function are explained below.


Some points to note:

i) Suppose frexp(x , *exp), returns ‘value‘ , than x=value * 2exp. A code exampleis gibven below.

int exp , exp1 ;
double value=frexp(23 , &exp) , 
 value1;

cout<< "value=" << value << endl
    << "exp=" << exp << endl ;

value1=frexp( 45.67 , &exp1 );

cout<< "\nvalue1=" << value1 << endl
 << "exp1=" << exp1 << endl ;

Output,

value=0.71875
exp=5
 
value=0.713594
exp=6

For the ouput to be correct they must satisfy ‘x=value * 2exp‘. So let us verify the above Output. Look at the code below.

Note, pow(2, x) means ‘2x‘.

cout<< "value*pow(2 , exp)=" << value*pow( 2, exp ) << endl
 << "value*pow(2 , exp1)=" << value1*pow( 2 ,exp1) << endl ;

Output,

value*pow(2 , exp)=23
value1*pow(2 , exp1)=45.67

Performing the reverse operations render the argument value, so we can be sure that the ouput of the previous program is correct.


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

int exp1 ;

cout<< "\nfrexp(0 , &exp1)=" << frexp(0 , &exp1 )
 << "\nexp1=" << exp1 << endl ;

Output,

frexp(0 , &exp1)=0
exp1=0


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

int exp1 ;

cout<< "\nfrexp(NAN , &exp2)=" << frexp( NAN , &exp2 )
 << "\nexp2=" << exp2 << endl ;

Output,

frexp(NAN , &exp2)=nan
exp2=82


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

int exp3;

cout<< "\nfrexp(INFINITY , &exp3)=" << frexp(INFINITY , &exp3 )
 << "\nexp3=" << exp3 << endl ;

Output,

frexp(INFINITY , &exp3)=inf
exp3=0


v) If int or character is passed the default double version i.e.e the 2nd function is called.

int exp1;

cout<< frexp( (int)89 , &exp1 ) //calls frexp(double , &exp)
 << "\n" << exp1 ;

Output,

0.695312
7


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

int exp1;

cout<< frexp( "345" , &exp1 ) //Error!!
   << "\n" << exp1 ;


C++ frexpf

The frexpf is same as the 1st function ‘ float frexp(float x, int* exp)’. This function accept only float type as the first argument. So if any argument type other than the float type is passed to frexpf then the argument is converted to the float type first.

int exp1;

cout<< frexpf( (long double)34 , &exp1 ) //calls frexpf(float, &exp1)
 << "\n" << exp1 ;

Output,

0.53125
6


C++ frexpl

The frexpl is same as the 3rd function ‘long double frexp(long double x, int* exp)’. This function accept only long double type as the first argument. So if any argument type other than the long double type is passed to frexpl then the argument is converted to the long double type first.

int exp1;

cout<< "frexpl( (float)90, &exp1 )=" << frexpl( (float)90 , &exp1 ) 
//calls frexpf(long double, &exp1)
 << "\nexp1=" << exp1 << endl ;

cout<< "\nfrexpl( ' ', &exp1 )=" << frexpl( ' ' , &exp1 ) 
//calls frexpf(long double, &exp1)
 << "\nexp1=" << exp1 ;

Output,

frexpl( (float)90 , &exp1 )=0.703125
exp1=7
 
frexpl( ‘L’ , &exp1 )=0.5
exp1=6




Leave a Reply

Your email address will not be published. Required fields are marked *