1 /* @(#)s_tanh.c 5.1 93/09/24 */
3 * ====================================================
4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
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
10 * ====================================================
14 * Return the Hyperbolic Tangent of x
19 * 0. tanhl(x) is defined to be -----------
22 * 1. reduce x to non-negative by tanhl(-x) = -tanhl(x).
23 * 2. 0 <= x <= 2**-55 : tanhl(x) := x*(one+x)
25 * 2**-55 < x <= 1 : tanhl(x) := -----; t = expm1l(-2x)
28 * 1 <= x <= 23.0 : tanhl(x) := 1- ----- ; t=expm1l(2x)
30 * 23.0 < x <= INF : tanhl(x) := 1.
34 * only tanhl(0)=0 is exact for finite argument.
39 #include "math_private.h"
41 static const long double one
=1.0, two
=2.0, tiny
= 1.0e-4900L;
50 /* High word of |x|. */
51 GET_LDOUBLE_WORDS(se
,jj0
,jj1
,x
);
56 /* for NaN it's not important which branch: tanhl(NaN) = NaN */
57 if (se
&0x8000) return one
/x
-one
; /* tanhl(-inf)= -1; */
58 else return one
/x
+one
; /* tanhl(+inf)=+1 */
62 if (ix
< 0x4003 || (ix
== 0x4003 && jj0
< 0xb8000000u
)) {/* |x|<23 */
63 if ((ix
|jj0
|jj1
) == 0)
64 return x
; /* x == +- 0 */
65 if (ix
<0x3fc8) /* |x|<2**-55 */
66 return x
*(one
+tiny
); /* tanh(small) = small */
67 if (ix
>=0x3fff) { /* |x|>=1 */
68 t
= expm1l(two
*fabsl(x
));
69 z
= one
- two
/(t
+two
);
71 t
= expm1l(-two
*fabsl(x
));
74 /* |x| > 23, return +-1 */
76 z
= one
- tiny
; /* raised inexact flag */
78 return (se
&0x8000)? -z
: z
;