6.11.145 pow Function

Calculates x raised to the power y.

Include

<math.h>

Prototype

double pow(double x, double y);

Arguments

x
the base
y
the exponent

Return Value

Returns x raised to the power y (xy).

Remarks

If x is finite and negative and y is finite and not an integer value, a domain error will occur and errno will be set to EDOM.

Example

See the notes at the beginning of this chapter or section for information on using printf() or scanf() (and other functions reading and writing the stdin or stdout streams) in the example code.

#include <math.h>
#include <stdio.h>
#include <errno.h>

int main(void)
{
  double x, y, z;

  errno = 0;
  x = -2.0;
  y = 3.1;
  z = pow(x, y);
  if (errno)
    perror("Error");
  printf("%f raised to %f is %f\n ", x, y, z);

  errno = 0;
  x = 3.0;
  y = -0.5;
  z = pow(x, y);
  if (errno)
    perror("Error");
  printf("%f raised to %f is %f\n ", x, y, z);

  errno = 0;
  x = 4.0;
  y = 0.0;
  z = pow(x, y);
  if (errno)
    perror("Error");
  printf("%f raised to %f is %f\n ", x, y, z);

  errno = 0;
  x = 0.0;
  y = -3.0;
  z = pow(x, y);
  if (errno)
    perror("Error");
  printf("%f raised to %f is %f\n ", x, y, z);
}

Example Output

Error: Domain error
-2.000000 raised to 3.100000 is nan
 3.000000 raised to -0.500000 is 0.577350
 4.000000 raised to 0.000000 is 1.000000
 0.000000 raised to -3.000000 is inf