C Library Function - tanh()
Description
C Library Function double tanh(double x) Return x The hyperbolic tangent of.
tan is a function in the C standard library is a function in the math library used to compute the hyperbolic tangent. The hyperbolic tangent function (tanh) has wide applications in many mathematical, physical, and engineering fields.
The hyperbolic tangent function is defined as:

Declaration
Below is the declaration of the tanh() function.
#include
double tanh(double x);
float tanhf(float x);
long double tanhl(long double x);
Parameters
- x -- The input real number, representing the argument of the hyperbolic tangent function.
Return Value
This function returns the hyperbolic tangent of x. The result is in the range [-1, 1].
Example
The following example demonstrates the use of the tanh() function.
Example
#include
#include
int
main
(
)
{
double
x
,
ret
;
x
=
0.5
;
ret
=
tanh
(
x
)
;
printf
(
"The hyperbolic tangent of %lf is %lf degrees"
,
x
,
ret
)
;
return
(
0
)
;
}
Let's compile and run the above program, which will produce the following result:
0.500000 的双曲正切是 0.462117 度
Calculate the hyperbolic tangent of multiple values
The following example shows how to calculate the hyperbolic tangent 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
=
tanh
(
x
)
;
printf
(
"tanh(%f) = %f\n"
,
x
,
result
)
;
}
return
0
;
}
Code Analysis
- Define an array containing multiple values
values. - Use
forLoop through each value, compute the hyperbolic tangent, and print the result.
Let's compile and run the above program, which will produce the following result:
tanh(0.000000) = 0.000000
tanh(0.500000) = 0.462117
tanh(1.000000) = 0.761594
tanh(1.500000) = 0.905148
tanh(2.000000) = 0.964028
Error handling
tanh() The function is valid for all real number inputs, so no special error handling is needed. The function does not set errnoand never returns NaN unless the input itself is NaN. In that case, the returned result will be NaN.
The following example shows how to handle special values (such as NaN and infinity):
Example
#include
#include
int
main
(
)
{
double
values
[
]
=
{
0
,
NAN
,
INFINITY
,
INFINITY
}
;
int
num_values
=
sizeof
(
values
)
/
sizeof
(
values
[
0
]
)
;
for
(
int
i
=
0
;
i
<
num_values
;
i
++
)
{
double
x
=
values
[
i
]
;
double
result
=
tanh
(
x
)
;
if
(
isnan
(
x
)
)
{
printf
(
"tanh(NaN) = NaN\n"
)
;
}
else
if
(
isinf
(
x
)
)
{
printf
(
"tanh(%f) = %f\n"
,
x
,
result
)
;
}
else
{
printf
(
"tanh(%f) = %f\n"
,
x
,
result
)
;
}
}
return
0
;
}
Code Analysis
- Define an array containing special values (such as NaN and infinity)
values. - Use
forLoop through each value, compute the hyperbolic tangent, and print the result. - Use
isnan()andisinf()The function checks whether the input value is NaN or infinity, and handles and prints the result accordingly.
Let's compile and run the above program, which will produce the following result:
tanh(0.000000) = 0.000000
tanh(NaN) = NaN
tanh(inf) = 1.000000
tanh(-inf) = -1.000000
Summary
tanh() The function is used to compute the hyperbolic tangent of a given value and is an important tool for handling hyperbolic function operations. Through the proper use of tanh() function, you can obtain accurate results in mathematical computations, physics simulations, and engineering applications.
C Standard Library -