pow, powf, powl

Header: <math.h>

1-3) Computes the value of base raised to the power exponent.

# Declarations

float powf( float base, float exponent );

(since C99)

double pow( double base, double exponent );
long double powl( long double base, long double exponent );

(since C99)

#define pow( base, exponent )

(since C99)

# Parameters

# Return value

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

# 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 <errno.h>
#include <fenv.h>
#include <math.h>
#include <stdio.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");
}

# See also