Namespaces
Variants
Views
Actions

pow

From cppreference.com
< c‎ | numeric‎ | math
 
 
 
Common mathematical functions
Functions
Basic operations
(C99)
(C99)
(C99)
(C99)
(C99)
(C99)(C99)(C99)
Exponential functions
(C99)
(C99)
(C99)
(C99)
Power functions
(C99)
(C99)
pow
Trigonometric and hyperbolic functions
(C99)
(C99)
(C99)
Error and gamma functions
(C99)
(C99)
(C99)
(C99)
Nearest integer floating point operations
(C99)(C99)(C99)
(C99)
(C99)(C99)(C99)
Floating point manipulation functions
(C99)(C99)
(C99)
(C99)
Classification
(C99)
(C99)
(C99)
Macro constants
 
Defined in header <math.h>
float powf( float base, float exp );
(1) (since C99)
double pow( double base, double exp );
(2)
long double powl( long double base, long double exp );
(3) (since C99)
Defined in header <tgmath.h>
#define pow( base, exp )
(4) (since C99)
1-3) Computes the value of base raised to the power exp.
4) Type-generic macro: If any argument has type long double, the long double version of the function is called. Otherwise, if any argument has type double, the double version of the function is called. Otherwise, the float version of the function is called. If at least one argument is complex, then the macro invokes the corresponding complex function (cpowf, cpow, cpowl)

Contents

[edit] Parameters

base - base as floating point value
exp - exponent as floating point value

[edit] Return value

base raised by power exp.

If base is finite and negative and exp is finite and non-integer, a domain error occurs and a range error may occur.

If base is zero and exp is zero, a domain error occurs.

If base is zero and exp is negative, a domain error or a pole error may occur.

If domain error occurs and math_errhandling & MATH_ERRNO is nonzero, errno is set to EDOM.

If domain error occurs and math_errhandling & MATH_ERREXCEPT is nonzero, FE_INVALID is raised.

If range error occurs and math_errhandling & MATH_ERRNO is nonzero, errno is set to ERANGE.

If range error occurs and math_errhandling & MATH_ERREXCEPT is nonzero, FE_OVERFLOW is raised.

If the implementation supports IEEE floating-point arithmetic (IEC 60559),

pow(+0, exp), where exp is a negative odd integer, returns +∞ and raises FE_DIVBYZERO
pow(-0, exp), where exp is a negative odd integer, returns -∞ and raises FE_DIVBYZERO
pow(±0, exp), where exp is negative, finite, and is an even integer or a non-integer, returns +∞ and raises FE_DIVBYZERO
pow(±0, -) returns +∞ and may raise FE_DIVBYZERO
pow(+0, exp), where exp is a positive odd integer, returns +0
pow(-0, exp), where exp is a positive odd integer, returns -0
pow(±0, exp), where exp is positive non-integer or a positive even integer, returns +0
pow(-1, ±∞) returns 1
pow(+1, exp) returns 1 for any exp, even when exp is NaN
pow(base, ±0) returns 1 for any base, even when base is NaN
pow(base, exp) returns NaN and raises FE_INVALID if base is finite and negative and exp is finite and non-integer.
pow(base, -) returns +∞ for any |base|<1
pow(base, -) returns +0 for any |base|>1
pow(base, +) returns +0 for any |base|<1
pow(base, +) returns +∞ for any |base|>1
pow(-∞, exp) returns -0 if exp is a negative odd integer
pow(-∞, exp) returns +0 if exp is a negative non-integer or even integer
pow(-∞, exp) returns -∞ if exp is a positive odd integer
pow(-∞, exp) returns +∞ if exp is a positive non-integer or even integer
pow(+∞, exp) returns +0 for any negative exp
pow(+∞, exp) returns +∞ for any positive exp
(since C99)

[edit] Example

#include <stdio.h>
#include <math.h>
#include <errno.h>
#include <fenv.h>
#include <string.h>
 
#pragma STDC FENV_ACCESS ON
 
void show_fe_exceptions(void)
{
    printf("current exceptions raised: ");
    if(fetestexcept(FE_DIVBYZERO))     printf(" FE_DIVBYZERO");
    if(fetestexcept(FE_INEXACT))       printf(" FE_INEXACT");
    if(fetestexcept(FE_INVALID))       printf(" FE_INVALID");
    if(fetestexcept(FE_OVERFLOW))      printf(" FE_OVERFLOW");
    if(fetestexcept(FE_UNDERFLOW))     printf(" FE_UNDERFLOW");
    if(fetestexcept(FE_ALL_EXCEPT)==0) printf(" none");
    printf("\n");
}
 
int main(void)
{
    printf("pow(2.0,2.0)      = %.1f\n", pow(2.0,2.0));
    printf("sqrt(4.0)         = %.1f\n", sqrt(4.0));        /* inverse of pow(x,2.0) */
    printf("pow(0.0,0.0)      = %.1f\n", pow(0.0,0.0));
    printf("pow(INFINITY,2.0) = %.1f\n", pow(INFINITY,2.0));
    printf("pow(NAN,2.0)      = %.1f\n", pow(NAN,2.0));
    printf("\n");
 
    /* overflow */
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("pow(2.0,1024.0)   = %g\n", pow(2.0,1024.0));
    printf("%s\n",strerror(errno));
    show_fe_exceptions();
    printf("\n");
 
    /* domain error                       */
    /* "invalid" floating-point exception */
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("pow(-1.0,2.1)     = %f\n", pow(-1.0,2.1));
    printf("%s\n",strerror(errno));
    show_fe_exceptions();
    printf("\n");
 
    /* pole error                                */
    /* "divide-by-zero" floating-point exception */
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("pow(0.0,-1.0)     = %f\n", pow(0.0,-1.0));
    printf("%s\n",strerror(errno));
    show_fe_exceptions();
    printf("\n");
 
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("pow(0.0,-2.0)     = %f\n", pow(0.0,-2.0));
    printf("%s\n",strerror(errno));
    show_fe_exceptions();
    printf("\n");
 
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("pow(0.0,-INFINITY) = %f\n", pow(0.0,-INFINITY));
    printf("%s\n",strerror(errno));
    show_fe_exceptions();
 
    return 0;
}

Possible output:

pow(2.0,2.0)      = 4.0
sqrt(4.0)         = 2.0
pow(0.0,0.0)      = 1.0
pow(INFINITY,2.0) = Inf
pow(NAN,2.0)      = NaN
 
pow(2.0,1024.0)   = Inf
Result too large
current exceptions raised:  FE_INEXACT FE_OVERFLOW
 
pow(-1.0,2.1)     = NaN
Argument out of domain
current exceptions raised:  FE_INVALID
 
pow(0.0,-1.0)     = -Inf
Argument out of domain
current exceptions raised:  FE_DIVBYZERO
 
pow(0.0,-2.0)     = -Inf
Argument out of domain
current exceptions raised:  FE_DIVBYZERO
 
pow(0.0,-INFINITY) = Inf
Result too large
current exceptions raised:  FE_DIVBYZERO

[edit] See also

returns e raised to the given power (ex)
(function) [edit]
computes natural (base e) logarithm (to base e) (ln(x))
(function) [edit]
computes square root (x)
(function) [edit]
(C99)
computes cubic root (3x)
(function) [edit]