C library function - exp()
Description
C Library Function double exp(double x) Return e 's x the value of e raised to the power of x.
exp() is a function in the C standard library is a function used to compute e raised to the power of x, where e is the base of the natural logarithm (approximately 2.71828). This is a commonly used mathematical function, widely applied in scientific computing, finance, engineering, and other fields.
Declaration
The following is the declaration of the exp() function.
#include
double exp(double x);
float expf(float x);
long double expl(long double x);
Parameters
x: The input real number, representing the exponent to compute.
Return Value
This function returns e raised to the power of x.
Example
The following example demonstrates the use of the exp() function.
Example
#include
#include
int
main
(
)
{
double
x
=
0
;
printf
(
"e to the power of %lf is %lf\n"
,
x
,
exp
(
x
)
)
;
printf
(
"e to the power of %lf is %lf\n"
,
x
1
,
exp
(
x
1
)
)
;
printf
(
"e to the power of %lf is %lf\n"
,
x
2
,
exp
(
x
2
)
)
;
return
(
0
)
;
}
Let's compile and run the above program, which will produce the following result:
e 的 0.000000 次幂是 1.000000
e 的 1.000000 次幂是 2.718282
e 的 2.000000 次幂是 7.389056
Computing the exponential of multiple values
The following example shows how to compute the exponential of multiple values:
Example
#include
#include
int
main
(
)
{
double
values
[
]
=
{
0
,
0.5
,
1
,
1.5
,
2
}
;
int
num_values
=
sizeof
(
values
)
/
sizeof
(
values
[
0
]
)
;
for
(
int
i
=
0
;
i
<
num_values
;
i
++
)
{
double
x
=
values
[
i
]
;
double
result
=
exp
(
x
)
;
printf
(
"exp(%f) = %f\n"
,
x
,
result
)
;
}
return
0
;
}
Let's compile and run the above program, which will produce the following result:
exp(0.000000) = 1.000000
exp(0.500000) = 1.648721
exp(1.000000) = 2.718282
exp(1.500000) = 4.481689
exp(2.000000) = 7.389056
Code Analysis
- Define an array containing multiple values
values. - Use
forLoop through each value, compute the exponential, and print the result.
Error handling
exp() The function may cause overflow when the input value is too large. In that case, the function returns positive infinity, and errno to ERANGE. You can use errno to check whether an overflow error has occurred.
The following example shows how to handle overflow errors:
Example
#include
#include
#include
int
main
(
)
{
double
x
=
1000.0
;
errno
=
0
;
// Reset errno
double
result
=
exp
(
x
)
;
if
(
errno
==
ERANGE
)
{
printf
(
"Overflow error: exp(%f) result is out of range.\n"
,
x
)
;
}
else
{
printf
(
"exp(%f) = %f\n"
,
x
,
result
)
;
}
return
0
;
}
Let's compile and run the above program, which will produce the following result:
exp(1000.000000) = inf
Code Analysis
- The code tries to compute
exp(1000.0), an input value that may cause overflow. errnois reset to 0, then callexp(x).- Checks
errnois equal toERANGE, and if so, print an overflow error message. - If there is no error, print the computation result.
Summary
exp() The function computes e raised to the power of x and is an important tool for exponential operations. When using it, note that possible overflow errors should be handled. Through appropriate use of exp() function, you can obtain accurate results in mathematical computations, physics simulations, and engineering applications.
C Standard Library -