C library function - cosh()
Description
C Library Function double cosh(double x) Return x The hyperbolic cosine of.
cosh() is a function in the C standard library is a function used to compute the hyperbolic cosine. The hyperbolic cosine function (cosh) is an important mathematical function with a wide range of uses in many scientific and engineering applications.
Declaration
The following is the declaration of the cosh() function.
#include
double cosh(double x);
float coshf(float x);
long double coshl(long double x);
Parameters
- x -- The input real number, representing the argument of the hyperbolic cosine function.
The hyperbolic cosine function is defined as:

Return Value
This function returns the hyperbolic cosine of x.
Example
The following example demonstrates the use of the cosh() function.
Example
#include
#include
int
main
(
)
{
double
x
;
x
=
0.5
;
printf
(
"The hyperbolic cosine of %lf is %lf.\n"
,
x
,
cosh
(
x
)
)
;
x
=
1.0
;
printf
(
"The hyperbolic cosine of %lf is %lf.\n"
,
x
,
cosh
(
x
)
)
;
x
=
1.5
;
printf
(
"The hyperbolic cosine of %lf is %lf.\n"
,
x
,
cosh
(
x
)
)
;
return
(
0
)
;
}
Let's compile and run the above program, which will produce the following result:
0.500000 的双曲余弦是 1.127626
1.000000 的双曲余弦是 1.543081
1.500000 的双曲余弦是 2.352410
Computing the hyperbolic cosine of multiple values
The following example shows how to compute the hyperbolic cosine 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
=
cosh
(
x
)
;
printf
(
"cosh(%f) = %f\n"
,
x
,
result
)
;
}
return
0
;
}
Let's compile and run the above program, which will produce the following result:
cosh(0.000000) = 1.000000
cosh(0.500000) = 1.127626
cosh(1.000000) = 1.543081
cosh(1.500000) = 2.352410
cosh(2.000000) = 3.762196
Code Analysis
- The code tries to compute
cosh(1000.0), an input value that may cause overflow. errnois reset to 0, then callcosh(x).- Checks
errnois equal toERANGE, and if so, print an overflow error message. - If there is no error, print the computation result.
Summary
cosh() The function computes the hyperbolic cosine of a given value and is an important tool for hyperbolic function operations. When using it, note that possible overflow errors should be handled. Through appropriate use of cosh() function, you can obtain accurate results in mathematical computations, physics simulations, and engineering applications.
C Standard Library -