C library functions -pow()

C standard library - <math.h>

Description

C Library Functionsdouble pow(double x, double y)returnxofypower, i.e., xy。

pow()is the C standard library<math.h>A function used to calculate the power of a number. Specifically, it returns the first argument raised to the power of the second argument, i.e.x^y。

Declaration

The following is the declaration of the pow() function.

#include <math.h>

double pow(double x, double y);
float powf(float x, float y);
long double powl(long double x, long double y);

Parameter

  • xBase, a floating-point number.
  • yExponent, a floating-point number.

Return Value

  • returnxofypower, i.e.x^y。
  • If the calculation produces a mathematical error, such as a non-integer power of a negative number, NaN will be returned, anderrno。

Error Handling

  • ifxis negative andynot an integer, the result is NaN,errnoSet toEDOM。
  • if the result is too large and causes overflow, returnsHUGE_VAL,errnoSet toERANGE。
  • if the result is too small and causes underflow, returns0.0,errnoSet toERANGE。

Example

The following example demonstrates the usage of the pow() function.

Example

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

int main ()
{
   printf("Value 8.0 ^ 3 = %lf\n", pow(8.0, 3));

   printf("value 3.05 ^ 1.98 = %lf", pow(3.05, 1.98));
   
   return(0);
}

Let us compile and run the above program, which will produce the following result:

值 8.0 ^ 3 = 512.000000
值 3.05 ^ 1.98 = 9.097324

handle multiple power calculations

The following example shows how to handle power calculations with multiple bases and exponents:

Example

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

int main() {
    double bases[] = {2.0, -2.0, 0.0, 2.0};
    double exponents[] = {3.0, 3.0, 0.0, -2.0};
    int num_values = sizeof(bases) / sizeof(bases[0]);

    for (int i = 0; i < num_values; i++) {
        double x = bases[i];
        double y = exponents[i];
        errno = 0;  // Reset errno
        double result = pow(x, y);

        if (errno == EDOM) {
            printf("Domain error: pow(%f, %f) is not defined\n", x, y);
        } else if (errno == ERANGE) {
            if (result == HUGE_VAL || result == -HUGE_VAL) {
                printf("Range error: pow(%f, %f) results in overflow\n", x, y);
            } else {
                printf("Range error: pow(%f, %f) results in underflow\n", x, y);
            }
        } else {
            printf("pow(%f, %f) = %f\n", x, y, result);
        }
    }

    return 0;
}

Let us compile and run the above program, which will produce the following result:

pow(2.000000, 3.000000) = 8.000000
pow(-2.000000, 3.000000) = -8.000000
pow(0.000000, 0.000000) = 1.000000
pow(2.000000, -2.000000) = 0.250000

Code explanation

  • define two arraysbasesandexponents, which respectively contain multiple bases and exponents.
  • UsageforLoop through each pair of base and exponent, callpow(x, y)perform power calculations.
  • reseterrnoto 0 and check for possible errors.
  • print the result or error message of each calculation.

Use Case

pow()The function has a wide range of uses in many applications, including but not limited to:

  • Calculate power operations in scientific formulas.
  • Solve mathematical problems involving power equations.
  • Calculate compound interest in finance.
  • Perform various physical calculations in engineering.

Summary

pow()The function is used to calculate the power of floating-point numbers and is an important tool for handling power operations. Through the reasonable use ofpow(), power operations can be implemented in scientific computing, engineering applications, and financial analysis, while ensuring that possible error conditions are handled properly.

C standard library - <math.h>

other extensions