YouTip LogoYouTip

C Function Tanh

[![Image 1: C Standard Library - ](#) C Standard Library - ](#) ## Description The C library function **double tanh(double x)** returns the hyperbolic tangent of **x**. `tanh` is a function in the C standard library `` used to compute the hyperbolic tangent value. The hyperbolic tangent function (tanh) has widespread applications in many mathematical, physical, and engineering contexts. The hyperbolic tangent function is defined as: !(#) ## Declaration Below is the declaration for the tanh() function. #include double tanh(double x);float tanhf(float x);long double tanhl(long double x); ## Parameters * **x** -- A real number input representing the argument of the hyperbolic tangent function. ## Return Value This function returns the hyperbolic tangent of x. The result lies in the range [-1, 1]. ## Example The following example demonstrates the usage of the tanh() function. ## Example #include #include int main () { double x, ret; x =0.5; ret =tanh(x); printf("%lf 's hyperbolic tangent is %lf degree", x, ret); return(0); } Let us compile and run the above program, which will produce the following result: 0.500000 's hyperbolic tangent is 0.462117 degree ### Computing Hyperbolic Tangent for Multiple Values The following example shows how to compute the hyperbolic tangent for multiple values: ## Example #include #include int main(){ double values[]={0,0.5,1,1.5,2}; int num_values =sizeof(values)/sizeof(values); for(int i =0; i < num_values; i++){ double x = values; double result =tanh(x); printf("tanh(%f) = %fn", x, result); } return 0; } ### Code Explanation * Define an array `values` containing multiple values. * Use a `for` loop to iterate over each value, compute its hyperbolic tangent, 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 The `tanh()` function is valid for all real-number inputs, so no special error handling is required. The function does not set `errno`, nor does 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 #include int main(){ double values[]={0, NAN, INFINITY,-INFINITY}; int num_values =sizeof(values)/sizeof(values); for(int i =0; i < num_values; i++){ double x = values; double result =tanh(x); if(isnan(x)){ printf("tanh(NaN) = NaNn"); }else if(isinf(x)){ printf("tanh(%f) = %fn", x, result); }else{ printf("tanh(%f) = %fn", x, result); } } return 0; } ### Code Explanation * Define an array `values` containing special values (such as NaN and infinity). * Use a `for` loop to iterate over each value, compute its hyperbolic tangent, and print the result. * Use the `isnan()` and `isinf()` functions to check whether the input value is NaN or infinity, and handle and print results 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 The `tanh()` function is used to compute the hyperbolic tangent of a given value and serves as an essential tool for performing hyperbolic function calculations. By using the `tanh()` function appropriately, accurate results can be obtained in mathematical computations, physical simulations, and engineering applications. [![Image 3: C Standard Library - ](#) C Standard Library - ](#)
← C Function ExpC Function Sinh β†’