1*2fe8fb19SBen Gras /* @(#)s_tanh.c 5.1 93/09/24 */
2*2fe8fb19SBen Gras /*
3*2fe8fb19SBen Gras * ====================================================
4*2fe8fb19SBen Gras * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5*2fe8fb19SBen Gras *
6*2fe8fb19SBen Gras * Developed at SunPro, a Sun Microsystems, Inc. business.
7*2fe8fb19SBen Gras * Permission to use, copy, modify, and distribute this
8*2fe8fb19SBen Gras * software is freely granted, provided that this notice
9*2fe8fb19SBen Gras * is preserved.
10*2fe8fb19SBen Gras * ====================================================
11*2fe8fb19SBen Gras */
12*2fe8fb19SBen Gras
13*2fe8fb19SBen Gras #include <sys/cdefs.h>
14*2fe8fb19SBen Gras #if defined(LIBM_SCCS) && !defined(lint)
15*2fe8fb19SBen Gras __RCSID("$NetBSD: s_tanh.c,v 1.10 2002/05/26 22:01:59 wiz Exp $");
16*2fe8fb19SBen Gras #endif
17*2fe8fb19SBen Gras
18*2fe8fb19SBen Gras /* Tanh(x)
19*2fe8fb19SBen Gras * Return the Hyperbolic Tangent of x
20*2fe8fb19SBen Gras *
21*2fe8fb19SBen Gras * Method :
22*2fe8fb19SBen Gras * x -x
23*2fe8fb19SBen Gras * e - e
24*2fe8fb19SBen Gras * 0. tanh(x) is defined to be -----------
25*2fe8fb19SBen Gras * x -x
26*2fe8fb19SBen Gras * e + e
27*2fe8fb19SBen Gras * 1. reduce x to non-negative by tanh(-x) = -tanh(x).
28*2fe8fb19SBen Gras * 2. 0 <= x <= 2**-55 : tanh(x) := x*(one+x)
29*2fe8fb19SBen Gras * -t
30*2fe8fb19SBen Gras * 2**-55 < x <= 1 : tanh(x) := -----; t = expm1(-2x)
31*2fe8fb19SBen Gras * t + 2
32*2fe8fb19SBen Gras * 2
33*2fe8fb19SBen Gras * 1 <= x <= 22.0 : tanh(x) := 1- ----- ; t=expm1(2x)
34*2fe8fb19SBen Gras * t + 2
35*2fe8fb19SBen Gras * 22.0 < x <= INF : tanh(x) := 1.
36*2fe8fb19SBen Gras *
37*2fe8fb19SBen Gras * Special cases:
38*2fe8fb19SBen Gras * tanh(NaN) is NaN;
39*2fe8fb19SBen Gras * only tanh(0)=0 is exact for finite argument.
40*2fe8fb19SBen Gras */
41*2fe8fb19SBen Gras
42*2fe8fb19SBen Gras #include "math.h"
43*2fe8fb19SBen Gras #include "math_private.h"
44*2fe8fb19SBen Gras
45*2fe8fb19SBen Gras static const double one=1.0, two=2.0, tiny = 1.0e-300;
46*2fe8fb19SBen Gras
47*2fe8fb19SBen Gras double
tanh(double x)48*2fe8fb19SBen Gras tanh(double x)
49*2fe8fb19SBen Gras {
50*2fe8fb19SBen Gras double t,z;
51*2fe8fb19SBen Gras int32_t jx,ix;
52*2fe8fb19SBen Gras
53*2fe8fb19SBen Gras /* High word of |x|. */
54*2fe8fb19SBen Gras GET_HIGH_WORD(jx,x);
55*2fe8fb19SBen Gras ix = jx&0x7fffffff;
56*2fe8fb19SBen Gras
57*2fe8fb19SBen Gras /* x is INF or NaN */
58*2fe8fb19SBen Gras if(ix>=0x7ff00000) {
59*2fe8fb19SBen Gras if (jx>=0) return one/x+one; /* tanh(+-inf)=+-1 */
60*2fe8fb19SBen Gras else return one/x-one; /* tanh(NaN) = NaN */
61*2fe8fb19SBen Gras }
62*2fe8fb19SBen Gras
63*2fe8fb19SBen Gras /* |x| < 22 */
64*2fe8fb19SBen Gras if (ix < 0x40360000) { /* |x|<22 */
65*2fe8fb19SBen Gras if (ix<0x3c800000) /* |x|<2**-55 */
66*2fe8fb19SBen Gras return x*(one+x); /* tanh(small) = small */
67*2fe8fb19SBen Gras if (ix>=0x3ff00000) { /* |x|>=1 */
68*2fe8fb19SBen Gras t = expm1(two*fabs(x));
69*2fe8fb19SBen Gras z = one - two/(t+two);
70*2fe8fb19SBen Gras } else {
71*2fe8fb19SBen Gras t = expm1(-two*fabs(x));
72*2fe8fb19SBen Gras z= -t/(t+two);
73*2fe8fb19SBen Gras }
74*2fe8fb19SBen Gras /* |x| > 22, return +-1 */
75*2fe8fb19SBen Gras } else {
76*2fe8fb19SBen Gras z = one - tiny; /* raised inexact flag */
77*2fe8fb19SBen Gras }
78*2fe8fb19SBen Gras return (jx>=0)? z: -z;
79*2fe8fb19SBen Gras }
80