1 
2 /* @(#)z_tanh.c 1.0 98/08/13 */
3 /*****************************************************************
4  * The following routines are coded directly from the algorithms
5  * and coefficients given in "Software Manual for the Elementary
6  * Functions" by William J. Cody, Jr. and William Waite, Prentice
7  * Hall, 1980.
8  *****************************************************************/
9 
10 /*
11 
12 FUNCTION
13         <<tanh>>, <<tanhf>>---hyperbolic tangent
14 
15 INDEX
16 tanh
17 INDEX
18 tanhf
19 
20 ANSI_SYNOPSIS
21         #include <math.h>
22         double tanh(double <[x]>);
23         float tanhf(float <[x]>);
24 
25 TRAD_SYNOPSIS
26         #include <math.h>
27         double tanh(<[x]>)
28         double <[x]>;
29 
30         float tanhf(<[x]>)
31         float <[x]>;
32 
33 
34 DESCRIPTION
35 
36 <<tanh>> computes the hyperbolic tangent of
37 the argument <[x]>.  Angles are specified in radians.
38 
39 <<tanh(<[x]>)>> is defined as
40 . sinh(<[x]>)/cosh(<[x]>)
41 
42 <<tanhf>> is identical, save that it takes and returns <<float>> values.
43 
44 RETURNS
45 The hyperbolic tangent of <[x]> is returned.
46 
47 PORTABILITY
48 <<tanh>> is ANSI C.  <<tanhf>> is an extension.
49 
50 */
51 
52 /******************************************************************
53  * Hyperbolic Tangent
54  *
55  * Input:
56  *   x - floating point value
57  *
58  * Output:
59  *   hyperbolic tangent of x
60  *
61  * Description:
62  *   This routine calculates hyperbolic tangent.
63  *
64  *****************************************************************/
65 
66 #include <float.h>
67 #include "fdlibm.h"
68 #include "zmath.h"
69 
70 #ifndef _DOUBLE_IS_32BITS
71 
72 static const double LN3_OVER2 = 0.54930614433405484570;
73 static const double p[] = { -0.16134119023996228053e+4,
74                             -0.99225929672236083313e+2,
75                             -0.96437492777225469787 };
76 static const double q[] = { 0.48402357071988688686e+4,
77                             0.22337720718962312926e+4,
78                             0.11274474380534949335e+3 };
79 
80 double
81 _DEFUN (tanh, (double),
82         double x)
83 {
84   double f, res, g, P, Q, R;
85 
86   f = fabs (x);
87 
88   /* Check if the input is too big. */
89   if (f > BIGX)
90     res = 1.0;
91 
92   else if (f > LN3_OVER2)
93     res = 1.0 - 2.0 / (exp (2 * f) + 1.0);
94 
95   /* Check if the input is too small. */
96   else if (f < z_rooteps)
97     res = f;
98 
99   /* Calculate the Taylor series. */
100   else
101     {
102       g = f * f;
103 
104       P = (p[2] * g + p[1]) * g + p[0];
105       Q = ((g + q[2]) * g + q[1]) * g + q[0];
106       R = g * (P / Q);
107 
108       res = f + f * R;
109     }
110 
111   if (x < 0.0)
112     res = -res;
113 
114   return (res);
115 }
116 
117 #endif /* _DOUBLE_IS_32BITS */
118