C++ cmath scalbn,scalbnf,scalbnl and scalbln,scalblnf,scalblnl function


The C++ <cmath> scalbn,scalbnf and scalbnl and scalbln,scalblnf and scalblnl compute the value of the first argument multiplied by the FLT_RADIX raise to the power of the second argument.The declaration of the functions are given below.

scalbn

1 float scalbn(float x, int exp);
2 double scalbn(double x, int exp);
3 long double scalbn(long double x, int exp);
4 float scalbnf(float x, int exp);
5 long double scalbnl(long double x, int exp);

scalbln

1 float scalbln(float x, int exp);
2 double scalbln(double x, int exp);
3 long double scalbln(long double x, int exp);
4 float scalblnf(float x, int exp);
5 long double scalblnl(long double x, int exp);

Parameters (scalbn and scalbln):
x -A floating point value.

exp -An intgeral value which is raised to the power of FLT_RADIX.

Return type
floating point -Returns the computed value of ‘x* FLT_RADIXexp‘.

Some points to note:

i) If the FLT_RADIX value is ‘2’ -which usually is- scalbn and scalbln is same as the ldexp function.

ii) If x or exp is very large ,the output value is ±∞ depending on the sign of the x.

iii) If x or exp is very small the output is ±0.

iv) If NAN is passed ‘nan’ is returned.

v) If ±∞ is passed ±∞ is returned.

Link: C++ cmath ldexp

Code example

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

/*Passing large value*/
cout<< “scalbn(-3e40 , 890)=” << scalbn(-3e40 , 890) <<endl;

/*Passing small value*/
cout<< “scalbln(1e-324 , 290)=” << scalbn(1e-324 , 290) <<endl;

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

/*Passing INFINITY*/
cout<< “scalbn( -INFINITY , 1)=” << scalbn(INFINITY , 890) <<endl;

Output

scalbn( 23 , 2)= 92
scalbln( 23 , 2)= 92
ldexp( 23 , 2)= 92
scalbn( -3e40 , 890)= -inf
scalbln( 1e-324 , 290)= 0
scalbn( NAN , 2)= nan
scalbn( -INFINITY , 1)= -inf


 


scalbnf and scalbnl

*Note (the explanation below also holds true for scalblnf and scalblnl)

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

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

Code example

#include <typeinfo>

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

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

Output

*scalbnf
f
f
*scalbnl
e (means ‘long double’)
e (means ‘long double’)