1*49393c00Smartynas /* @(#)s_tanh.c 5.1 93/09/24 */
2*49393c00Smartynas /*
3*49393c00Smartynas * ====================================================
4*49393c00Smartynas * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5*49393c00Smartynas *
6*49393c00Smartynas * Developed at SunPro, a Sun Microsystems, Inc. business.
7*49393c00Smartynas * Permission to use, copy, modify, and distribute this
8*49393c00Smartynas * software is freely granted, provided that this notice
9*49393c00Smartynas * is preserved.
10*49393c00Smartynas * ====================================================
11*49393c00Smartynas */
12*49393c00Smartynas
13*49393c00Smartynas /*
14*49393c00Smartynas * Copyright (c) 2008 Stephen L. Moshier <steve@moshier.net>
15*49393c00Smartynas *
16*49393c00Smartynas * Permission to use, copy, modify, and distribute this software for any
17*49393c00Smartynas * purpose with or without fee is hereby granted, provided that the above
18*49393c00Smartynas * copyright notice and this permission notice appear in all copies.
19*49393c00Smartynas *
20*49393c00Smartynas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
21*49393c00Smartynas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
22*49393c00Smartynas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
23*49393c00Smartynas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
24*49393c00Smartynas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
25*49393c00Smartynas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
26*49393c00Smartynas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
27*49393c00Smartynas */
28*49393c00Smartynas
29*49393c00Smartynas /* tanhl(x)
30*49393c00Smartynas * Return the Hyperbolic Tangent of x
31*49393c00Smartynas *
32*49393c00Smartynas * Method :
33*49393c00Smartynas * x -x
34*49393c00Smartynas * e - e
35*49393c00Smartynas * 0. tanhl(x) is defined to be -----------
36*49393c00Smartynas * x -x
37*49393c00Smartynas * e + e
38*49393c00Smartynas * 1. reduce x to non-negative by tanhl(-x) = -tanhl(x).
39*49393c00Smartynas * 2. 0 <= x <= 2**-57 : tanhl(x) := x*(one+x)
40*49393c00Smartynas * -t
41*49393c00Smartynas * 2**-57 < x <= 1 : tanhl(x) := -----; t = expm1l(-2x)
42*49393c00Smartynas * t + 2
43*49393c00Smartynas * 2
44*49393c00Smartynas * 1 <= x <= 40.0 : tanhl(x) := 1- ----- ; t=expm1l(2x)
45*49393c00Smartynas * t + 2
46*49393c00Smartynas * 40.0 < x <= INF : tanhl(x) := 1.
47*49393c00Smartynas *
48*49393c00Smartynas * Special cases:
49*49393c00Smartynas * tanhl(NaN) is NaN;
50*49393c00Smartynas * only tanhl(0)=0 is exact for finite argument.
51*49393c00Smartynas */
52*49393c00Smartynas
53*49393c00Smartynas #include "math.h"
54*49393c00Smartynas #include "math_private.h"
55*49393c00Smartynas
56*49393c00Smartynas static const long double one = 1.0, two = 2.0, tiny = 1.0e-4900L;
57*49393c00Smartynas
58*49393c00Smartynas long double
tanhl(long double x)59*49393c00Smartynas tanhl(long double x)
60*49393c00Smartynas {
61*49393c00Smartynas long double t, z;
62*49393c00Smartynas u_int32_t jx, ix;
63*49393c00Smartynas ieee_quad_shape_type u;
64*49393c00Smartynas
65*49393c00Smartynas /* Words of |x|. */
66*49393c00Smartynas u.value = x;
67*49393c00Smartynas jx = u.parts32.mswhi;
68*49393c00Smartynas ix = jx & 0x7fffffff;
69*49393c00Smartynas /* x is INF or NaN */
70*49393c00Smartynas if (ix >= 0x7fff0000)
71*49393c00Smartynas {
72*49393c00Smartynas /* for NaN it's not important which branch: tanhl(NaN) = NaN */
73*49393c00Smartynas if (jx & 0x80000000)
74*49393c00Smartynas return one / x - one; /* tanhl(-inf)= -1; */
75*49393c00Smartynas else
76*49393c00Smartynas return one / x + one; /* tanhl(+inf)=+1 */
77*49393c00Smartynas }
78*49393c00Smartynas
79*49393c00Smartynas /* |x| < 40 */
80*49393c00Smartynas if (ix < 0x40044000)
81*49393c00Smartynas {
82*49393c00Smartynas if (u.value == 0)
83*49393c00Smartynas return x; /* x == +- 0 */
84*49393c00Smartynas if (ix < 0x3fc60000) /* |x| < 2^-57 */
85*49393c00Smartynas return x * (one + tiny); /* tanh(small) = small */
86*49393c00Smartynas u.parts32.mswhi = ix; /* Absolute value of x. */
87*49393c00Smartynas if (ix >= 0x3fff0000)
88*49393c00Smartynas { /* |x| >= 1 */
89*49393c00Smartynas t = expm1l (two * u.value);
90*49393c00Smartynas z = one - two / (t + two);
91*49393c00Smartynas }
92*49393c00Smartynas else
93*49393c00Smartynas {
94*49393c00Smartynas t = expm1l (-two * u.value);
95*49393c00Smartynas z = -t / (t + two);
96*49393c00Smartynas }
97*49393c00Smartynas /* |x| > 40, return +-1 */
98*49393c00Smartynas }
99*49393c00Smartynas else
100*49393c00Smartynas {
101*49393c00Smartynas z = one - tiny; /* raised inexact flag */
102*49393c00Smartynas }
103*49393c00Smartynas return (jx & 0x80000000) ? -z : z;
104*49393c00Smartynas }
105