C library functions -tanh()
Description
C Library Functionsdouble tanh(double x)returnxthe hyperbolic tangent of.
tanis part of the C standard library.<math.h>A function used to compute the hyperbolic tangent value. The hyperbolic tangent function (tanh) has broad applications in many mathematical, physical, and engineering fields.
The definition of the hyperbolic tangent function is:

Declaration
The following is the declaration of the tanh() function.
#include <math.h> double tanh(double x); float tanhf(float x); long double tanhl(long double x);
Parameter
- x-- The input real number, representing the argument of the hyperbolic tangent function.
Return Value
This function returns the hyperbolic tangent of x. The range of the result is [-1, 1].
Example
The following example demonstrates the usage of the tanh() function.
Example
#include <math.h>
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 us 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 demonstrates how to compute the hyperbolic tangent of multiple values:
Example
#include <math.h>
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 explanation
- Define an array containing multiple values
values。 - Usage
forLoop through each value, calculate the hyperbolic tangent value, and print the result.
Let us 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 inputs, so no special error handling is required. The function does not seterrno, nor will it return NaN unless the input itself is NaN. In that case, the returned result will be NaN.
The following example demonstrates how to handle special values (such as NaN and infinity):
Example
#include <math.h>
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 explanation
- Define an array containing special values (such as NaN and infinity)
values。 - Usage
forLoop through each value, calculate the hyperbolic tangent value, and print the result. - Usage
isnan()andisinf()The function checks whether the input value is NaN or infinity, and processes and prints the result accordingly.
Let us 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 calculate the hyperbolic tangent of a given value, and is an important tool for handling hyperbolic function operations. Through reasonable usetanh()The function can yield accurate results in mathematical calculations, physical simulations, and engineering applications.