1 /* origin: FreeBSD /usr/src/lib/msun/src/s_ctanh.c */
3 * Copyright (c) 2011 David Schultz
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice unmodified, this list of conditions, and the following
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * Hyperbolic tangent of a complex argument z = x + i y.
31 * The algorithm is from:
33 * W. Kahan. Branch Cuts for Complex Elementary Functions or Much
34 * Ado About Nothing's Sign Bit. In The State of the Art in
35 * Numerical Analysis, pp. 165 ff. Iserles and Powell, eds., 1987.
46 * tanh(z) = sinh(z) / cosh(z)
48 * sinh(x) cos(y) + i cosh(x) sin(y)
49 * = ---------------------------------
50 * cosh(x) cos(y) + i sinh(x) sin(y)
52 * cosh(x) sinh(x) / cos^2(y) + i tan(y)
53 * = -------------------------------------
54 * 1 + sinh^2(x) / cos^2(y)
62 * I omitted the original algorithm's handling of overflow in tan(x) after
63 * verifying with nearpi.c that this can't happen in IEEE single or double
64 * precision. I also handle large x differently.
69 double complex ctanh(double complex z)
72 double t, beta, s, rho, denom;
78 EXTRACT_WORDS(hx, lx, x);
82 * ctanh(NaN + i 0) = NaN + i 0
84 * ctanh(NaN + i y) = NaN + i NaN for y != 0
86 * The imaginary part has the sign of x*sin(2*y), but there's no
87 * special effort to get this right.
89 * ctanh(+-Inf +- i Inf) = +-1 +- 0
91 * ctanh(+-Inf + i y) = +-1 + 0 sin(2y) for y finite
93 * The imaginary part of the sign is unspecified. This special
94 * case is only needed to avoid a spurious invalid exception when
97 if (ix >= 0x7ff00000) {
98 if ((ix & 0xfffff) | lx) /* x is NaN */
99 return cpack(x, (y == 0 ? y : x * y));
100 SET_HIGH_WORD(x, hx - 0x40000000); /* x = copysign(1, x) */
101 return cpack(x, copysign(0, isinf(y) ? y : sin(y) * cos(y)));
105 * ctanh(x + i NAN) = NaN + i NaN
106 * ctanh(x +- i Inf) = NaN + i NaN
109 return cpack(y - y, y - y);
112 * ctanh(+-huge + i +-y) ~= +-1 +- i 2sin(2y)/exp(2x), using the
113 * approximation sinh^2(huge) ~= exp(2*huge) / 4.
114 * We use a modified formula to avoid spurious overflow.
116 if (ix >= 0x40360000) { /* x >= 22 */
117 double exp_mx = exp(-fabs(x));
118 return cpack(copysign(1, x), 4 * sin(y) * cos(y) * exp_mx * exp_mx);
121 /* Kahan's algorithm */
123 beta = 1.0 + t * t; /* = 1 / cos^2(y) */
125 rho = sqrt(1 + s * s); /* = cosh(x) */
126 denom = 1 + beta * s * s;
127 return cpack((beta * rho * s) / denom, t / denom);