1 | |
---|
2 | /* @(#)s_tanh.c 5.1 93/09/24 */ |
---|
3 | /* |
---|
4 | * ==================================================== |
---|
5 | * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. |
---|
6 | * |
---|
7 | * Developed at SunPro, a Sun Microsystems, Inc. business. |
---|
8 | * Permission to use, copy, modify, and distribute this |
---|
9 | * software is freely granted, provided that this notice |
---|
10 | * is preserved. |
---|
11 | * ==================================================== |
---|
12 | */ |
---|
13 | |
---|
14 | /* |
---|
15 | |
---|
16 | FUNCTION |
---|
17 | <<tanh>>, <<tanhf>>---hyperbolic tangent |
---|
18 | |
---|
19 | INDEX |
---|
20 | tanh |
---|
21 | INDEX |
---|
22 | tanhf |
---|
23 | |
---|
24 | SYNOPSIS |
---|
25 | #include <math.h> |
---|
26 | double tanh(double <[x]>); |
---|
27 | float tanhf(float <[x]>); |
---|
28 | |
---|
29 | DESCRIPTION |
---|
30 | |
---|
31 | <<tanh>> computes the hyperbolic tangent of |
---|
32 | the argument <[x]>. Angles are specified in radians. |
---|
33 | |
---|
34 | <<tanh(<[x]>)>> is defined as |
---|
35 | . sinh(<[x]>)/cosh(<[x]>) |
---|
36 | |
---|
37 | <<tanhf>> is identical, save that it takes and returns <<float>> values. |
---|
38 | |
---|
39 | RETURNS |
---|
40 | The hyperbolic tangent of <[x]> is returned. |
---|
41 | |
---|
42 | PORTABILITY |
---|
43 | <<tanh>> is ANSI C. <<tanhf>> is an extension. |
---|
44 | |
---|
45 | */ |
---|
46 | |
---|
47 | /* Tanh(x) |
---|
48 | * Return the Hyperbolic Tangent of x |
---|
49 | * |
---|
50 | * Method : |
---|
51 | * x -x |
---|
52 | * e - e |
---|
53 | * 0. tanh(x) is defined to be ----------- |
---|
54 | * x -x |
---|
55 | * e + e |
---|
56 | * 1. reduce x to non-negative by tanh(-x) = -tanh(x). |
---|
57 | * 2. 0 <= x <= 2**-55 : tanh(x) := x*(one+x) |
---|
58 | * -t |
---|
59 | * 2**-55 < x <= 1 : tanh(x) := -----; t = expm1(-2x) |
---|
60 | * t + 2 |
---|
61 | * 2 |
---|
62 | * 1 <= x <= 22.0 : tanh(x) := 1- ----- ; t=expm1(2x) |
---|
63 | * t + 2 |
---|
64 | * 22.0 < x <= INF : tanh(x) := 1. |
---|
65 | * |
---|
66 | * Special cases: |
---|
67 | * tanh(NaN) is NaN; |
---|
68 | * only tanh(0)=0 is exact for finite argument. |
---|
69 | */ |
---|
70 | |
---|
71 | #include "fdlibm.h" |
---|
72 | |
---|
73 | #ifndef _DOUBLE_IS_32BITS |
---|
74 | |
---|
75 | #ifdef __STDC__ |
---|
76 | static const double one=1.0, two=2.0, tiny = 1.0e-300; |
---|
77 | #else |
---|
78 | static double one=1.0, two=2.0, tiny = 1.0e-300; |
---|
79 | #endif |
---|
80 | |
---|
81 | #ifdef __STDC__ |
---|
82 | double tanh(double x) |
---|
83 | #else |
---|
84 | double tanh(x) |
---|
85 | double x; |
---|
86 | #endif |
---|
87 | { |
---|
88 | double t,z; |
---|
89 | __int32_t jx,ix; |
---|
90 | |
---|
91 | /* High word of |x|. */ |
---|
92 | GET_HIGH_WORD(jx,x); |
---|
93 | ix = jx&0x7fffffff; |
---|
94 | |
---|
95 | /* x is INF or NaN */ |
---|
96 | if(ix>=0x7ff00000) { |
---|
97 | if (jx>=0) return one/x+one; /* tanh(+-inf)=+-1 */ |
---|
98 | else return one/x-one; /* tanh(NaN) = NaN */ |
---|
99 | } |
---|
100 | |
---|
101 | /* |x| < 22 */ |
---|
102 | if (ix < 0x40360000) { /* |x|<22 */ |
---|
103 | if (ix<0x3c800000) /* |x|<2**-55 */ |
---|
104 | return x*(one+x); /* tanh(small) = small */ |
---|
105 | if (ix>=0x3ff00000) { /* |x|>=1 */ |
---|
106 | t = expm1(two*fabs(x)); |
---|
107 | z = one - two/(t+two); |
---|
108 | } else { |
---|
109 | t = expm1(-two*fabs(x)); |
---|
110 | z= -t/(t+two); |
---|
111 | } |
---|
112 | /* |x| > 22, return +-1 */ |
---|
113 | } else { |
---|
114 | z = one - tiny; /* raised inexact flag */ |
---|
115 | } |
---|
116 | return (jx>=0)? z: -z; |
---|
117 | } |
---|
118 | |
---|
119 | #endif /* _DOUBLE_IS_32BITS */ |
---|