# C++ cmath ldexp,ldexpf and ldexpl function

The C++ <cmath> ldexp,ldexpf and ldexpl function multiply the floating point number with the integral power of 2.The declaration for the functions are given below.

1 float ldexp(float x, int exp);
2 double ldexp(double x, int exp);
3 long double ldexp(long double x, int exp);
4 float ldexpf(float x, int exp);
5 long double ldexpl(long double x, int exp);

Parameters:
x – A floating point number.

exp -The value which will be raised as the power to 2.

Return type
floating point -Returns x multiplied by 2 to the power exp.

Some points to note:

i) ldexp(x,exp) is same as x * 2exp.

ii) If ‘NAN’ is passed ‘nan’ is returned,if INFINITY if passed ‘inf’ is returned.

Code example

/*Passing floating point value*/
cout<< “ldexp(23,2)=” << ldexp(23 , 2) << endl ;

/*Passing NAN*/
cout<< “ldexp(NAN,2)=” << ldexp(NAN , 2) << endl ;

/*Passing INFINITY */
cout<< “ldexp(INFINITY , 2)=” << ldexp(INFINITY , 2) << endl ;

Output

ldexp(23,2)= 92
ldexp(NAN,2)= nan
ldexp(INFINITY,2)= inf

This function is similar to scalbn and scalbln functions.

#### ldexpf and ldexpl

*Note

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

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

Code example

#include <typeinfo>

/*ldexpf */
cout<< “*ldexpf\n”;
float f=2;
cout<< typeid( ldexpf(2 ,4) ).name() << endl /*identify type of ldexpf retunred value */
<< typeid( ldexp(f,4) ).name() << endl; /*identify type of ldexp(float) returned value */

/*ldexpl*/
cout<< “*ldexpl\n”;
long double ld=2;
cout<< typeid( ldexpl(2,4) ).name() << endl /*indentify type of ldexpl returned value */
<< typeid( ldexp(ld,4) ).name() << endl; /*identify type of ldexp(long double) returned value*/

Output

*ldexpf
f
f
*ldexpl
e (means ‘long double’)
e (means ‘long double’)