pow, powf, powl

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

Parameters

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

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.

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),

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.

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

References

See also

sqrtsqrtfsqrtl
(C99)(C99)
computes square root (\(\small{\sqrt{x} }\)x)
(function)
cbrtcbrtfcbrtl
(C99)(C99)(C99)
computes cube root (\(\small{\sqrt[3]{x} }\)3x)
(function)
hypothypotfhypotl
(C99)(C99)(C99)
computes square root of the sum of the squares of two given numbers (\(\scriptsize{\sqrt{x^2+y^2} }\)x2+y2)
(function)
cpowcpowfcpowl
(C99)(C99)(C99)
computes the complex power function
(function)
C++ documentation for pow

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/c/numeric/math/pow