4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
26 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
27 * Use is subject to license terms.
30 #pragma weak __ctanh = ctanh
34 * dcomplex ctanh(dcomplex z);
36 * tanh x + i tan y sinh 2x + i sin 2y
37 * ctanh z = --------------------- = --------------------
38 * 1 + i tanh(x)tan(y) cosh 2x + cos 2y
40 * For |x| >= prec/2 (14,28,34,60 for single, double, double extended, quad),
44 * cosh 2x = sinh 2x = --- e and hence ctanh z = 1 + i -----------;
48 * otherwise, to avoid cancellation, for |x| < prec/2,
51 * cosh 2x + cos 2y = 1 + ------------ + cos y - sin y
56 * = --- (e - 1) e + 2 cos y
62 * sinh 2x = --- [ e - 1 + --------- ]
66 * Implementation notes: let t = expm1(2x) = e - 1, then
69 * cosh 2x + cos 2y = --- * [ ----- + 4 cos y ]; sinh 2x = --- * [ t + --- ]
75 * t*t+2t [4(t+1)(cos y)]*(sin y)
76 * ctanh z = --------------------------- + i --------------------------
77 * t*t+[4(t+1)(cos y)](cos y) t*t+[4(t+1)(cos y)](cos y)
79 * EXCEPTION (conform to ISO/IEC 9899:1999(E)):
81 * ctanh(x,inf) = (NaN,NaN) for finite x
82 * ctanh(x,NaN) = (NaN,NaN) for finite x
83 * ctanh(inf,y) = 1+ i*0*sin(2y) for positive-signed finite y
84 * ctanh(inf,inf) = (1, +-0)
85 * ctanh(inf,NaN) = (1, +-0)
86 * ctanh(NaN,0) = (NaN,0)
87 * ctanh(NaN,y) = (NaN,NaN) for non-zero y
88 * ctanh(NaN,NaN) = (NaN,NaN)
92 #include "libm.h" /* exp/expm1/fabs/sin/tanh/sincos */
93 #include "complex_wrapper.h"
95 static const double four
= 4.0, two
= 2.0, one
= 1.0, zero
= 0.0;
99 double t
, r
, v
, u
, x
, y
, S
, C
;
100 int hx
, ix
, lx
, hy
, iy
, ly
;
107 ix
= hx
& 0x7fffffff;
110 iy
= hy
& 0x7fffffff;
114 if ((iy
| ly
) == 0) { /* ctanh(x,0) = (x,0) for x = 0 or NaN */
117 } else if (iy
>= 0x7ff00000) { /* y is inf or NaN */
118 if (ix
< 0x7ff00000) /* catanh(finite x,inf/nan) is nan */
119 D_RE(ans
) = D_IM(ans
) = y
- y
;
120 else if (((ix
- 0x7ff00000) | lx
) == 0) { /* x is inf */
127 } else if (ix
>= 0x403c0000) {
129 * |x| > 28 = prec/2 (14,28,34,60)
130 * ctanh z ~ 1 + i (sin2y)/(exp(2x))
133 if (iy
< 0x7fe00000) /* t = sin(2y) */
136 (void) sincos(y
, &S
, &C
);
139 if (ix
>= 0x7fe00000) { /* |x| > max/2 */
140 if (ix
>= 0x7ff00000) { /* |x| is inf or NaN */
141 if (((ix
- 0x7ff00000) | lx
) != 0)
142 D_RE(ans
) = D_IM(ans
) = x
+ y
;
145 D_IM(ans
) = zero
* S
; /* x is inf */
147 D_IM(ans
) = S
* exp(-x
); /* underflow */
149 D_IM(ans
) = (S
+ S
) * exp(-(x
+ x
));
150 /* 2 sin 2y / exp(2x) */
155 * ctanh z = --------------------------- +
156 * t*t+[4(t+1)(cos y)](cos y)
158 * [4(t+1)(cos y)]*(sin y)
159 * i --------------------------
160 * t*t+[4(t+1)(cos y)](cos y)
163 (void) sincos(y
, &S
, &C
);
165 r
= (four
* C
) * (t
+ one
);
167 v
= one
/ (u
+ r
* C
);
168 D_RE(ans
) = (u
+ two
* t
) * v
;
169 D_IM(ans
) = (r
* S
) * v
;
172 D_RE(ans
) = -D_RE(ans
);
174 D_IM(ans
) = -D_IM(ans
);