Rev 1906 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3362 | Serge | 1 | |
2 | /* |
||
3 | * ==================================================== |
||
4 | * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. |
||
5 | * |
||
6 | * Developed at SunPro, a Sun Microsystems, Inc. business. |
||
7 | * Permission to use, copy, modify, and distribute this |
||
8 | * software is freely granted, provided that this notice |
||
9 | * is preserved. |
||
10 | * ==================================================== |
||
11 | */ |
||
12 | |||
13 | |||
14 | |||
15 | |||
16 | < |
||
17 | |||
18 | |||
19 | tanh |
||
20 | INDEX |
||
21 | tanhf |
||
22 | |||
23 | |||
24 | #include |
||
25 | double tanh(double <[x]>); |
||
26 | float tanhf(float <[x]>); |
||
27 | |||
28 | |||
29 | #include |
||
30 | double tanh(<[x]>) |
||
31 | double <[x]>; |
||
32 | |||
33 | |||
34 | float <[x]>; |
||
35 | |||
36 | |||
37 | |||
38 | |||
39 | |||
40 | the argument <[x]>. Angles are specified in radians. |
||
41 | |||
42 | |||
43 | . sinh(<[x]>)/cosh(<[x]>) |
||
44 | |||
45 | |||
46 | |||
47 | |||
48 | The hyperbolic tangent of <[x]> is returned. |
||
49 | |||
50 | |||
51 | < |
||
52 | |||
53 | |||
54 | |||
55 | |||
56 | * Return the Hyperbolic Tangent of x |
||
57 | * |
||
58 | * Method : |
||
59 | * x -x |
||
60 | * e - e |
||
61 | * 0. tanh(x) is defined to be ----------- |
||
62 | * x -x |
||
63 | * e + e |
||
64 | * 1. reduce x to non-negative by tanh(-x) = -tanh(x). |
||
65 | * 2. 0 <= x <= 2**-55 : tanh(x) := x*(one+x) |
||
66 | * -t |
||
67 | * 2**-55 < x <= 1 : tanh(x) := -----; t = expm1(-2x) |
||
68 | * t + 2 |
||
69 | * 2 |
||
70 | * 1 <= x <= 22.0 : tanh(x) := 1- ----- ; t=expm1(2x) |
||
71 | * t + 2 |
||
72 | * 22.0 < x <= INF : tanh(x) := 1. |
||
73 | * |
||
74 | * Special cases: |
||
75 | * tanh(NaN) is NaN; |
||
76 | * only tanh(0)=0 is exact for finite argument. |
||
77 | */ |
||
78 | |||
79 | |||
80 | |||
81 | |||
82 | |||
83 | |||
84 | static const double one=1.0, two=2.0, tiny = 1.0e-300; |
||
85 | #else |
||
86 | static double one=1.0, two=2.0, tiny = 1.0e-300; |
||
87 | #endif |
||
88 | |||
89 | |||
90 | double tanh(double x) |
||
91 | #else |
||
92 | double tanh(x) |
||
93 | double x; |
||
94 | #endif |
||
95 | { |
||
96 | double t,z; |
||
97 | __int32_t jx,ix; |
||
98 | |||
99 | |||
100 | GET_HIGH_WORD(jx,x); |
||
101 | ix = jx&0x7fffffff; |
||
102 | |||
103 | |||
104 | if(ix>=0x7ff00000) { |
||
105 | if (jx>=0) return one/x+one; /* tanh(+-inf)=+-1 */ |
||
106 | else return one/x-one; /* tanh(NaN) = NaN */ |
||
107 | } |
||
108 | |||
109 | |||
110 | if (ix < 0x40360000) { /* |x|<22 */ |
||
111 | if (ix<0x3c800000) /* |x|<2**-55 */ |
||
112 | return x*(one+x); /* tanh(small) = small */ |
||
113 | if (ix>=0x3ff00000) { /* |x|>=1 */ |
||
114 | t = expm1(two*fabs(x)); |
||
115 | z = one - two/(t+two); |
||
116 | } else { |
||
117 | t = expm1(-two*fabs(x)); |
||
118 | z= -t/(t+two); |
||
119 | } |
||
120 | /* |x| > 22, return +-1 */ |
||
121 | } else { |
||
122 | z = one - tiny; /* raised inexact flag */ |
||
123 | } |
||
124 | return (jx>=0)? z: -z; |
||
125 | } |
||
126 | |||
127 | |||
128 |