C programming frexp,frexpf and frexpl function


In C programming the <math.h> frexp,frexpf and frexpl functions 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.

All the functions are the same,they compute the base-2 exponential of ‘x’ minus 1.The 1st,2nd and the 3rd function have the same name i.e.’frexp’.The 4th and 5th function have different names.What does having a different names mean is explained in detail below.

Some points to note:

i) frexp(x , *exp), returns ‘value‘ ,such that 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

double d=12 ;

int exp=0 , x1=90 ;

float f=12 ;

long double ld=12 ;

printf(“%lf”, frexp( d , exp) ) ; //calls the 2nd version
printf(“\n%f”, frexp( f , exp) ) ; //calls the 1st version
printf(“\n%Lf”, frexp( ld , exp) ) ;//calls the 3rd version
printf( “\nexp=%i” , exp);

printf(“\n%lf”, frexp( x1 , exp ) ) ;
printf( “\nexp=%i” , exp);
printf(“\n%lf”, frexp(INFINITY , exp) ) ;
printf( “\nexp=%i” , exp);

Output in Visual Studio,

0.750000
0.750000
0.750000
exp=4
0.703125
exp=7
-nan(ind)
exp=-1

If INFINITY(macro for infinity value) is passed ‘-nan(ind)'(NaN) is returned and the ‘exp’ value is -1.Note here the compiler is Visual Studio that is why it gives -1,other compiler will yield different values.



frexpf

The ‘frexpf‘ function is same as the 1st function.This means the parameter and return type of the function is float type.The ‘f‘ character appended to ‘frexpf‘ stands for float and it signify the parameter and return type of the function.

Code example

float x=400 ;

int exp=0 ;

printf(“%f”, frexp( x , exp ) ); //Calls the 1st version
printf(“\n%f”, frexpf( 400 , exp ) ); Calls the 4th version
printf(“\nexp=%i” , exp);

Output,

0.781250
0.781250
exp=9


frexpl

The ‘frexpl‘ function is same as the 1st function.This means the parameter and return type of the function is float type.The ‘l‘ character appended to ‘frexpl‘ stands for float and it signify the parameter and return type of the function.

Code example

float x=400 ;

int exp=0 ;

printf(“%Lf”, frexp( x , exp ) ); //Calls the 1st version
printf(“\n%Lf”, frexpl( 400 , exp ) ); Calls the 4th version
printf(“\nexp=%i” , exp);

Output,

0.781250
0.781250
exp=9