Namespaces
Variants
Views
Actions

pow, powf, powl

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 exponent );
(1) (since C99)
double pow( double base, double exponent );
(2)
long double powl( long double base, long double exponent );
(3) (since C99)
Defined in header <tgmath.h>
#define pow( base, exponent )
(4) (since C99)
1-3) Computes the value of base raised to the power exponent.
4) Type-generic macro: If any argument has type long double, powl is called. Otherwise, if any argument has integer type or has type double, pow is called. Otherwise, powf is called. If at least one argument is complex or imaginary, then the macro invokes the corresponding complex function (cpowf, cpow, cpowl).

Contents

[edit] Parameters

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

[edit] Return value

If no errors occur, base raised to the power of exponent (baseexponent
) is returned.

If a domain error occurs, an implementation-defined value is returned (NaN where supported).

If a pole error or a range error due to overflow occurs, ±HUGE_VAL, ±HUGE_VALF, or ±HUGE_VALL is returned.

If a range error occurs due to underflow, the correct result (after rounding) is returned.

[edit] Error handling

Errors are reported as specified in math_errhandling.

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

If base is zero and exponent is zero, a domain error may occur.

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

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

  • pow(+0, exponent), where exponent is a negative odd integer, returns +∞ and raises FE_DIVBYZERO
  • pow(-0, exponent), where exponent is a negative odd integer, returns -∞ and raises FE_DIVBYZERO
  • pow(±0, exponent), where exponent 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, exponent), where exponent is a positive odd integer, returns +0
  • pow(-0, exponent), where exponent is a positive odd integer, returns -0
  • pow(±0, exponent), where exponent is positive non-integer or a positive even integer, returns +0
  • pow(-1, ±∞) returns 1
  • pow(+1, exponent) returns 1 for any exponent, even when exponent is NaN
  • pow(base, ±0) returns 1 for any base, even when base is NaN
  • pow(base, exponent) returns NaN and raises FE_INVALID if base is finite and negative and exponent 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(-∞, exponent) returns -0 if exponent is a negative odd integer
  • pow(-∞, exponent) returns +0 if exponent is a negative non-integer or even integer
  • pow(-∞, exponent) returns -∞ if exponent is a positive odd integer
  • pow(-∞, exponent) returns +∞ if exponent is a positive non-integer or even integer
  • pow(+∞, exponent) returns +0 for any negative exponent
  • pow(+∞, exponent) returns +∞ for any positive exponent
  • except where specified above, if any argument is NaN, NaN is returned

[edit] Notes

Although pow cannot be used to obtain a root of a negative number, cbrt is provided for the common case where exponent is 1/3.

[edit] Example

#include <stdio.h>
#include <math.h>
#include <errno.h>
#include <fenv.h>
 
#pragma STDC FENV_ACCESS ON
int main(void)
{
    // typical usage
    printf("pow(2, 10) = %f\n", pow(2,10));
    printf("pow(2, 0.5) = %f\n", pow(2,0.5));
    printf("pow(-2, -3) = %f\n", pow(-2,-3));
    // special values
    printf("pow(-1, NAN) = %f\n", pow(-1,NAN));
    printf("pow(+1, NAN) = %f\n", pow(+1,NAN));
    printf("pow(INFINITY, 2) = %f\n", pow(INFINITY, 2));
    printf("pow(INFINITY, -1) = %f\n", pow(INFINITY, -1));
    // error handling 
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("pow(-1, 1/3) = %f\n", pow(-1, 1.0/3));
    if(errno == EDOM)         perror("    errno == EDOM");
    if(fetestexcept(FE_INVALID)) puts("    FE_INVALID raised");
 
    feclearexcept(FE_ALL_EXCEPT);
    printf("pow(-0, -3) = %f\n", pow(-0.0, -3));
    if(fetestexcept(FE_DIVBYZERO)) puts("    FE_DIVBYZERO raised");
}

Possible output:

pow(2, 10) = 1024.000000
pow(2, 0.5) = 1.414214
pow(-2, -3) = -0.125000
pow(-1, NAN) = nan
pow(+1, NAN) = 1.000000
pow(INFINITY, 2) = inf
pow(INFINITY, -1) = 0.000000
pow(-1, 1/3) = -nan
    errno == EDOM: Numerical argument out of domain
    FE_INVALID raised
pow(-0, -3) = -inf
    FE_DIVBYZERO raised

[edit] See also

(C99)(C99)
computes square root (x)
(function) [edit]
(C99)(C99)(C99)
computes cubic root (3x)
(function) [edit]
(C99)(C99)(C99)
computes square root of the sum of the squares of two given numbers (x2
+y2
)
(function) [edit]
(C99)(C99)(C99)
computes the complex power function
(function) [edit]