C++ cmath pow , powf and powl function


The C++ <cmath> pow,powf and powl compute the resultant output of the value with power raised to certain 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’.

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

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

cout<< “pow(5 , -3)=” << pow(5 , -3) << endl;

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

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

Output

pow(2 , 4)= 16
pow(5 , -3)= 0.008
pow(NAN , NAN)= nan
pow(INFINITY , INFINITY)= inf






powf and powl

*Note

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

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

Code example

#include <typeinfo>

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

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

Output

*powf
f
f
*powl
e (means ‘long double’)
e (means ‘long double’)


*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).