Description
The C library function double pow(double x, double y) returns x raised to the power of y, i.e., xy.
pow() is a function in the C standard library <math.h> used to compute the power of a number. Specifically, it returns the first argument raised to the power of the second argument, i.e., x^y.
Declaration
Below 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);
Parameters
x: the base (radix), a floating-point number.y: the exponent, a floating-point number.
Return Value
- Returns
xraised to the power ofy, i.e.,x^y. - If the calculation results in a mathematical error, such as raising a negative number to a non-integer power, NaN is returned and
errnois set.
Error Handling
- If
xis negative andyis not an integer, the result is NaN anderrnois set toEDOM. - If the result is too large causing overflow,
HUGE_VALis returned anderrnois set toERANGE. - If the result is too small causing underflow,
0.0is returned anderrnois set 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 = %lfn", pow(8.0,3));
printf("Value 3.05 ^ 1.98 = %lf", pow(3.05,1.98));
return(0);
}
Let's compile and run the above program, which will produce the following result:
Value 8.0 ^ 3 = 512.000000
Value 3.05 ^ 1.98 = 9.097324
Handling Multiple Power Calculations
The following example shows how to handle power calculations for 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);
for(int i = 0; i < num_values; i++){
double x = bases;
double y = exponents;
errno = 0; // Reset errno
double result = pow(x, y);
if(errno == EDOM){
printf("Domain error: pow(%f, %f) is not definedn", x, y);
} else if(errno == ERANGE){
if(result == HUGE_VAL || result == -HUGE_VAL){
printf("Range error: pow(%f, %f) results in overflown", x, y);
} else {
printf("Range error: pow(%f, %f) results in underflown", x, y);
}
} else {
printf("pow(%f, %f) = %fn", x, y, result);
}
}
return 0;
}
Let's 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
- Two arrays
basesandexponentsare defined, containing multiple bases and exponents respectively. - A
forloop iterates over each pair of base and exponent, callingpow(x, y)to perform the power calculation. errnois reset to 0 and possible errors are checked.- The result of each calculation or any error message is printed.
Use Cases
The pow() function has wide-ranging applications, including but not limited to:
- Performing exponentiation in scientific formulas.
- Solving mathematical problems involving power equations.
- Calculating compound interest in finance.
- Conducting various physical calculations in engineering.
Summary
The pow() function is used to calculate the power of floating-point numbers and is an essential tool for handling exponentiation. By using pow() appropriately, power operations can be implemented in scientific computing, engineering applications, and financial analysis, while properly handling potential error conditions.
YouTip