1 | |
---|
2 | /* @(#)z_tanhf.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 | * Hyperbolic Tangent |
---|
11 | * |
---|
12 | * Input: |
---|
13 | * x - floating point value |
---|
14 | * |
---|
15 | * Output: |
---|
16 | * hyperbolic tangent of x |
---|
17 | * |
---|
18 | * Description: |
---|
19 | * This routine calculates hyperbolic tangent. |
---|
20 | * |
---|
21 | *****************************************************************/ |
---|
22 | |
---|
23 | #include <float.h> |
---|
24 | #include "fdlibm.h" |
---|
25 | #include "zmath.h" |
---|
26 | |
---|
27 | static const float LN3_OVER2 = 0.5493061443; |
---|
28 | static const float p[] = { -0.2059432032, |
---|
29 | -0.0009577527 }; |
---|
30 | static const float q[] = { 0.6178299136, |
---|
31 | 0.25 }; |
---|
32 | |
---|
33 | float |
---|
34 | tanhf (float x) |
---|
35 | { |
---|
36 | float f, res, g, P, Q, R; |
---|
37 | |
---|
38 | f = fabsf (x); |
---|
39 | |
---|
40 | /* Check if the input is too big. */ |
---|
41 | if (f > BIGX) |
---|
42 | res = 1.0; |
---|
43 | |
---|
44 | else if (f > LN3_OVER2) |
---|
45 | res = 1.0 - 2.0 / (exp (2 * f) + 1.0); |
---|
46 | |
---|
47 | /* Check if the input is too small. */ |
---|
48 | else if (f < z_rooteps_f) |
---|
49 | res = f; |
---|
50 | |
---|
51 | /* Calculate the Taylor series. */ |
---|
52 | else |
---|
53 | { |
---|
54 | g = f * f; |
---|
55 | |
---|
56 | P = p[1] * g + p[0]; |
---|
57 | Q = (g + q[1]) * g + q[0]; |
---|
58 | R = g * (P / Q); |
---|
59 | |
---|
60 | res = f + f * R; |
---|
61 | } |
---|
62 | |
---|
63 | if (x < 0.0) |
---|
64 | res = -res; |
---|
65 | |
---|
66 | return (res); |
---|
67 | } |
---|
68 | |
---|
69 | #ifdef _DOUBLE_IS_32BITS |
---|
70 | |
---|
71 | double tanh (double x) |
---|
72 | { |
---|
73 | return (double) tanhf ((float) x); |
---|
74 | } |
---|
75 | |
---|
76 | #endif _DOUBLE_IS_32BITS |
---|