C Library Function - pow()
Description
C Library Function double pow(double x, double y) Return x 's y power, i.e., xy.
pow() is a function in the C standard library a function in the C standard library 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
The following is the declaration of the pow() function.
#include
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, a floating-point number.y: The exponent, a floating-point number.
Return Value
- Return
x'sypower, i.e.,x^y. - If the calculation produces a mathematical error, such as a negative number raised to a non-integer power, NaN is returned and
errno.
Error handling
- If both the
xis negative andyis not an integer, the result is NaN,errnotoEDOM. - If the result is too large and causes overflow, it returns
HUGE_VAL,errnotoERANGE. - If the result is too small and causes underflow, it returns
0.0,errnotoERANGE.
Example
The following example demonstrates the use of the pow() function.
Example
#include
#include
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's compile and run the above program, which will produce the following result:
值 8.0 ^ 3 = 512.000000
值 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
#include
#include
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'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 Analysis
- Define two arrays
basesandexponents, containing multiple bases and exponents respectively. - Use
forLoop through each pair of base and exponent, callingpow(x, y)to perform the power calculation. - Reset
errnoto 0 and check for possible errors. - Print the result or error message of each calculation.
Use Cases
pow() The function has a wide range of uses in many applications, including but not limited to:
- Compute powers in scientific formulas.
- Solve mathematical problems involving power equations.
- Calculate compound interest in finance.
- Perform various physics calculations in engineering.
Summary
pow() The function is used to compute the power of a floating-point number and is an important tool for handling exponentiation. Through proper use of pow(), exponentiation can be achieved in scientific computing, engineering applications, and financial analysis, and possible error conditions can be handled properly.
C Standard Library -