pow, powf, powl - cppreference.com (original) (raw)

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.

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

[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 <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"); }

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] References

[edit] See also