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 * Copyright (c) 2008 Stephen L. Moshier <steve@moshier.net>
16 * Permission to use, copy, modify, and distribute this software for any
17 * purpose with or without fee is hereby granted, provided that the above
18 * copyright notice and this permission notice appear in all copies.
20 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
21 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
22 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
23 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
24 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
25 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
26 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
30 * Return the Hyperbolic Tangent of x
35 * 0. tanhl(x) is defined to be -----------
38 * 1. reduce x to non-negative by tanhl(-x) = -tanhl(x).
39 * 2. 0 <= x <= 2**-57 : tanhl(x) := x*(one+x)
41 * 2**-57 < x <= 1 : tanhl(x) := -----; t = expm1l(-2x)
44 * 1 <= x <= 40.0 : tanhl(x) := 1- ----- ; t=expm1l(2x)
46 * 40.0 < x <= INF : tanhl(x) := 1.
50 * only tanhl(0)=0 is exact for finite argument.
53 #include <openlibm_math.h>
55 #include "math_private.h"
57 static const long double one
= 1.0, two
= 2.0, tiny
= 1.0e-4900L;
64 ieee_quad_shape_type u
;
73 /* for NaN it's not important which branch: tanhl(NaN) = NaN */
75 return one
/ x
- one
; /* tanhl(-inf)= -1; */
77 return one
/ x
+ one
; /* tanhl(+inf)=+1 */
84 return x
; /* x == +- 0 */
85 if (ix
< 0x3fc60000) /* |x| < 2^-57 */
86 return x
* (one
+ tiny
); /* tanh(small) = small */
87 u
.parts32
.mswhi
= ix
; /* Absolute value of x. */
90 t
= expm1l (two
* u
.value
);
91 z
= one
- two
/ (t
+ two
);
95 t
= expm1l (-two
* u
.value
);
98 /* |x| > 40, return +-1 */
102 z
= one
- tiny
; /* raised inexact flag */
104 return (jx
& 0x80000000) ? -z
: z
;