C programming scalbn,scalbnf,scalbnl and scalbln,scalblnf,scalblnl math.h

In C programming the <math.h> 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 (for both 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) The FLT_RADIX value may vary from compiler to compiler and if it’s 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.

Code example

double d=12 , x1=-2e200 ;

int exp=2 ;

float f=12 ;

long double ld=12 ;

/** Checking the value of FLT_RADIX **/

/** using scalbn **/
printf(“\n\n%lf”, scalbn( d , exp ) ) ; //calls the 2nd version
printf(“\n%f”, scalbn( f , exp ) ) ; //calls the 1st version
printf(“\n%Lf”, scalbn( ld , exp ) ) ;//calls the 3rd version

/** Using scalbln **/
printf(“\n\n%lf”, scalbln( d , exp ) ) ; //calls the 2nd version
printf(“\n%f”, scalbln( f , exp ) ) ; //calls the 1st version
printf(“\n%Lf”, scalbln( ld , exp ) ) ;//calls the 3rd version

printf(“\n%lf”, scalbn( x1 , exp ) ) ;
printf(“\n%lf”, scalbln( INFINITY , exp ) ) ;

Output in Visual Studio,

96.000000
96.000000
96.000000

96.000000
96.000000
96.000000

-0.000000
inf

From the output we can see that Visual Studio has FLT_RADIX value as 2.Since x1 is very small negative value on passing it as argument we get the output as -0.000000 and in passing INFINITY we get ‘inf'(infinity).

scalbnf and scalblnf

The ‘scalbnf‘ function is same as the ‘float scalbn(float)’.And ‘scalblnf‘ is same as the ‘float scalbln(float)’.The ‘f‘ character appended to ‘scalbnf‘ and ‘scalblnf‘ stands for float and it signify the parameter type and return type of the function.

Code example

float x=40 ;

int exp=3 ;

printf(“%f”, scalbnf( x , exp) ); //Calls the 4th version
printf(“\n%f”, scalblnf( 40 , exp) ); Calls the 4th version

Output,

320.000000
320.000000

scalbnl and scalblnl

The ‘scalbnl‘ function is same as the ‘long double scalbn(long double)’.And ‘scalblnl‘ is same as the ‘long double scalbln(long double)’.The ‘l‘ character appended to ‘scalbnl‘ and ‘scalblnl‘ stands for long double and it signify the parameter type and return type of the function.

Code example

long double x=40 ;

int exp=3 ;

printf(“%Lf”, scalbnl( x , exp) ); //Calls the 5th version
printf(“\n%Lf”, scalblnl( 40 , exp ) ); Calls the 5th version

Output ,

320.000000
320.000000