2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2011 David Schultz
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * Hyperbolic tangent of a complex argument z = x + I y.
32 * The algorithm is from:
34 * W. Kahan. Branch Cuts for Complex Elementary Functions or Much
35 * Ado About Nothing's Sign Bit. In The State of the Art in
36 * Numerical Analysis, pp. 165 ff. Iserles and Powell, eds., 1987.
47 * tanh(z) = sinh(z) / cosh(z)
49 * sinh(x) cos(y) + I cosh(x) sin(y)
50 * = ---------------------------------
51 * cosh(x) cos(y) + I sinh(x) sin(y)
53 * cosh(x) sinh(x) / cos^2(y) + I tan(y)
54 * = -------------------------------------
55 * 1 + sinh^2(x) / cos^2(y)
63 * I omitted the original algorithm's handling of overflow in tan(x) after
64 * verifying with nearpi.c that this can't happen in IEEE single or double
65 * precision. I also handle large x differently.
68 #include <sys/cdefs.h>
72 #include "math_private.h"
75 ctanh(double complex z)
78 double t, beta, s, rho, denom;
84 EXTRACT_WORDS(hx, lx, x);
88 * ctanh(NaN +- I 0) = d(NaN) +- I 0
90 * ctanh(NaN + I y) = d(NaN,y) + I d(NaN,y) for y != 0
92 * The imaginary part has the sign of x*sin(2*y), but there's no
93 * special effort to get this right.
95 * ctanh(+-Inf +- I Inf) = +-1 +- I 0
97 * ctanh(+-Inf + I y) = +-1 + I 0 sin(2y) for y finite
99 * The imaginary part of the sign is unspecified. This special
100 * case is only needed to avoid a spurious invalid exception when
103 if (ix >= 0x7ff00000) {
104 if ((ix & 0xfffff) | lx) /* x is NaN */
105 return (CMPLX(nan_mix(x, y),
106 y == 0 ? y : nan_mix(x, y)));
107 SET_HIGH_WORD(x, hx - 0x40000000); /* x = copysign(1, x) */
108 return (CMPLX(x, copysign(0, isinf(y) ? y : sin(y) * cos(y))));
112 * ctanh(+-0 + i NAN) = +-0 + i NaN
113 * ctanh(+-0 +- i Inf) = +-0 + i NaN
114 * ctanh(x + i NAN) = NaN + i NaN
115 * ctanh(x +- i Inf) = NaN + i NaN
118 return (CMPLX(x ? y - y : x, y - y));
121 * ctanh(+-huge +- I y) ~= +-1 +- I 2sin(2y)/exp(2x), using the
122 * approximation sinh^2(huge) ~= exp(2*huge) / 4.
123 * We use a modified formula to avoid spurious overflow.
125 if (ix >= 0x40360000) { /* |x| >= 22 */
126 double exp_mx = exp(-fabs(x));
127 return (CMPLX(copysign(1, x),
128 4 * sin(y) * cos(y) * exp_mx * exp_mx));
131 /* Kahan's algorithm */
133 beta = 1.0 + t * t; /* = 1 / cos^2(y) */
135 rho = sqrt(1 + s * s); /* = cosh(x) */
136 denom = 1 + beta * s * s;
137 return (CMPLX((beta * rho * s) / denom, t / denom));
141 ctan(double complex z)
144 /* ctan(z) = -I * ctanh(I * z) = I * conj(ctanh(I * conj(z))) */
145 z = ctanh(CMPLX(cimag(z), creal(z)));
146 return (CMPLX(cimag(z), creal(z)));