pow(), powf(), powl() — Raise to power
Standards
Standards / Extensions | C or C++ | Dependencies |
---|---|---|
ISO C
POSIX.1 XPG4 XPG4.2 ISO/ANSI C++ C99 Single UNIX Specification, Version 3 C++ TR1 C99 |
both |
Format
#include <math.h>
double pow(double x, double y);
double pow(double x, int y); /* C++ only */
float pow(float x, int y); /* C++ only */
float pow(float x, float y); /* C++ only */
long double pow(long double x, int y); /* C++ only */
long double pow(long double x, long double y); /* C++ only */
float powf(float x, float y);
long double powl(long double x, long double y);
General description
The pow(),
powf(), and powl() functions calculate the value of x to
the power of y.
Note: These functions work
in both IEEE Binary Floating-Point and hexadecimal floating-point
formats. See IEEE binary floating-point for more information
about IEEE Binary Floating-Point.
Restriction: The powf() function does not support the _FP_MODE_VARIABLE
feature test macro.
Returned value
If successful, the pow(), powf(), and powl() functions return the value of x to the power of y.
If y is 0, the function returns 1.
If x is negative and y is non-integral, the function sets errno to EDOM and returns -HUGE_VAL. If the correct value is outside the range of representable values, ±HUGE_VAL is returned according to the sign of the value, and the value of ERANGE is stored in errno.
Special behavior for IEEE: If x is negative or 0, then the y parameter must be an integer. If y is 0, the function returns 1.0 for all x parameters.
If an overflow occurs, the function returns HUGE_VAL and sets errno to ERANGE.
If both x and y are negative, the function returns NaNQ and sets errno to EDOM.
If x is 0 and y is
negative, the function returns HUGE_VAL and does not modify errno,
but powf sets errno to ERANGE.
Note: When environment variable _EDC_SUSV3
is set to 2, and if x is 0 and y is
negative, the function returns -HUGE_VAL and sets errno to ERANGE.
Example
CELEBP05
/* CELEBP05
This example calculates the value of 2**3.
*/
#include <math.h>
#include <stdio.h>
int main(void)
{
double x, y, z;
x = 2.0;
y = 3.0;
z = pow(x,y);
printf("%lf to the power of %lf is %lf\n", x, y, z);
}
Output:
2.000000 to the power of 3.000000 is 8.000000