C programming pow , powf and powl math.h


In C programming the <math.h> pow,powf and powl compute the resultant output of the value with power raised to the other value.The declaration of the functions are given below.

1 float pow(float x , float y);
2 double pow(double x , double y);
3 long double pow(long double x , long double y);
4 float powf(float x , float y);
5 long double powl(long double x , long double y);


Parameters:
x -The base value.

y -The value which is raised as the power.

Return type
floating point -The value of ‘y’ raised to the power of ‘x’.

All the functions are the same,they compute the second argument raised as the power to the first argument.The 1st,2nd and the 3rd function have the same name i.e.’pow’.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) pow(x,y) is same as xy.

ii) A domain error occurs if x is finite and negative and y is finite and not an integer value,for instance pow(-2, 4.5),gives ‘nan’.

iii) A domain error may occur if x and y is 0.

iv) A domain error or range error may occur if x is 0 and y is less than 0,for instance pow(0 , -7 ),gives ‘inf’.

v) If both the arguments are NAN ,the function returns ‘nan’.If both are ∞ ,it returns ∞.

A more vivid discussion of each case for each parameter type is given as a “Side note” below.

Code example

double dx=2 ,dy=4 , x1=-2 , x2=2.98 ;

float fx=2 , fy=4 ;

long double ldx=2 , ldy=4 ;

printf(“%lf”, pow( dx , dy ) ) ; //calls the 2nd version
printf(“\n%f”, pow( fx , fy ) ) ; //calls the 1st version
printf(“\n%Lf”, pow( ldx ,ldy ) ) ;//calls the 3rd version

printf(“\n%lf”, pow( x1 , x2 ) ) ;
printf(“\n%lf”, pow( INFINITY , 2 ) ) ;

Output in Visual Studio,

16.000000
16.000000
16.000000
-nan(ind)
inf

For the fourth output the value ‘-nan(ind)'(or simply NaN) signify that a domain error has occurred.






powf

The ‘powf‘ 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 ‘powf‘ stands for float and it signify the parameter type and return type of the function.

Code example

float x=0.32 , y=0.45 ;

printf(“%f”, pow( x , y ) ); //Calls the 1st version
printf(“\n%f”, powf( 0.32 , 0.45 ) ); Calls the 4th version

Output in Visual Studio,

0.598849
0.598849


powl

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

Code example

long double x=0.32 , y=0.45 ;

printf(“%Lf”, pow( x ) ); //Calls the 3rd version
printf(“\n%Lf”, powl( 0.32 , 0.45 ) ); Calls the 5th version

Output,

0.598849
0.598849


*Side Note

Facts about pow function:

  i) pow(±0 , y) returns ±∞ and raises the ‘divide-by-zero’ floating-point exception for y an odd integer <0.For instance pow(-0.0 , -3) gives ‘-inf’.

  ii) pow(±0 , y) returns +∞ and raises the ‘divide-by-zero’ floating-point exception for y<0 and not an odd integer.For instance pow(-0.0 , -4) ,give ‘inf’.

  iii) pow(±0 , y) returns ±0 for y an odd integer > 0.For instance pow(-0.0, -7) ,give -0.

  iv) pow(±0 , y) returns +0 for y>0 and y is not an odd integer.For instance pow(-0.0 , 4),gives 0.

  v) pow(−1 , ±∞) returns 1., e.g pow(-1 , INFINITY).

  vi) pow(+1 , y) returns 1 for any y,even a NAN, e.g. pow(1 , NAN) ,gives 1.

  vii) pow(x , ±0)returns 1 for any x,even a NAN ,e.g. pow(34 , -0.0),gives 1.

  viii) pow(x , y) returns a NAN and raises the ‘invalid’ floating-point exception for finite x<0 and finite non-integer y.For instance pow(-9 , .3) ,gives ‘nan’.

when y is ±∞

  ix) pow(x , −∞) returns +∞ for |x|<1, e.g. pow(-0.3 , -INFINITY).

  x) pow(x , −∞) returns +0 for |x|>1, e.g. pow(5 , -INFINITY).

  xi) pow(x , +∞) returns +0 for |x|<1, e.g. pow( 0.9 , INFINITY).

  xii) pow(x , +∞) returns +∞ for |x|>1 , e.g. pow(7 , INFINITY).

when x is ±∞

  xiii) pow(−∞ , y) returns −0 for y an odd integer < 0 ,e.g. pow(-INFINITY , -3).

  xiv) pow(−∞ , y) returns +0 for y<0 and not an odd integer ,e.g. pow(-INFINITY , -4).

  xv) pow(−∞ , y) returns -∞ for y an odd integer > 0, e.g. pow(-INFINITY , 3).

  xvi) pow(−∞ , y) returns +∞ for y>0 and not an odd integer,e.g. pow(-INFINITY , 4).

  xvii) pow(+∞ , y) returns +0 for y<0, e.g. pow(INFINITY , -2).

  xviii) pow(+∞ , y) returns +∞ for y>0, e.g. pow(INFINITY , 2).