
- C Library - Home
- C Library - <assert.h>
- C Library - <complex.h>
- C Library - <ctype.h>
- C Library - <errno.h>
- C Library - <fenv.h>
- C Library - <float.h>
- C Library - <inttypes.h>
- C Library - <iso646.h>
- C Library - <limits.h>
- C Library - <locale.h>
- C Library - <math.h>
- C Library - <setjmp.h>
- C Library - <signal.h>
- C Library - <stdalign.h>
- C Library - <stdarg.h>
- C Library - <stdbool.h>
- C Library - <stddef.h>
- C Library - <stdio.h>
- C Library - <stdlib.h>
- C Library - <string.h>
- C Library - <tgmath.h>
- C Library - <time.h>
- C Library - <wctype.h>
- C Programming Resources
- C Programming - Tutorial
- C - Useful Resources
C library - tanh() function
The C library tanh() function accepts the parameter(x) of type double returns the hyperbolic tangent of x.
The parameter passed to tanh() can be any real number, either its positive or negative. The real life uses of tanh() function − Neural Network, Signal Processing, statistics and data analysis.
Syntax
Following is the syntax of the C library function tanh() −
double tanh(double x)
Parameters
This function takes only a single parameters −
x − This is the floating point value.
Return Value
This function returns hyperbolic tangent of x.
Example 1
Following is the C library program to see the demonstration of tanh() function.
#include <stdio.h> #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); }
Output
The above code produces the following result −
The hyperbolic tangent of 0.500000 is 0.462117 degrees
Example 2
In this program, the angle set to 30 degrees as an input. So, we can adjust the value of angle_degrees to explore different angles and observe their corresponding hyperbolic tangent values using tanh().
#include <stdio.h> #include <math.h> int main() { // angle in degree double angle_deg = 30.0; // Convert degrees to radians double angle_radians = angle_deg * (M_PI / 180.0); // Calculate tanh double res = tanh(angle_radians); printf("tanh(%.2lf degrees) = %.4lf\n", angle_deg, res); return 0; }
Output
On execution of above code, we get the following result −
tanh(30.00 degrees) = 0.4805
Example 3
Below the example decribe the approximation of e^x using tanh(). Here, we define the custom function my_exp() that use the formula identity ex = 1 + tanh(x/2) / 1 - tanh(x/2) to return the result.
#include <stdio.h> #include <math.h> double my_exp(double x) { return (1.0 + tanh(x / 2.0)) * (1.0 - tanh(x / 2.0)); } int main() { double x = 2.0; double apx_exp = my_exp(x); printf("Approximate e^%.2lf = %.6lf\n", x, apx_exp); return 0; }
Output
On execution of above code, we get the following result −
Approximate e^2.00 = 0.419974 Approximate e^2.00 = 0.419974