blob: e5840a1bf67b823512aca9e5ac4e3c93f3f5aa25 [file] [log] [blame]
Elliott Hughesa0ee0782013-01-30 19:06:37 -08001/*-
Elliott Hughes8da8ca42018-05-08 13:35:33 -07002 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
Elliott Hughesa0ee0782013-01-30 19:06:37 -08004 * Copyright (c) 2011 David Schultz
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
12 * disclaimer.
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.
16 *
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.
27 */
28
29/*
Elliott Hughes8cff2f92015-08-28 20:21:43 -070030 * Hyperbolic tangent of a complex argument z = x + I y.
Elliott Hughesa0ee0782013-01-30 19:06:37 -080031 *
32 * The algorithm is from:
33 *
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.
37 *
38 * Method:
39 *
40 * Let t = tan(x)
41 * beta = 1/cos^2(y)
42 * s = sinh(x)
43 * rho = cosh(x)
44 *
45 * We have:
46 *
47 * tanh(z) = sinh(z) / cosh(z)
48 *
Elliott Hughes8cff2f92015-08-28 20:21:43 -070049 * sinh(x) cos(y) + I cosh(x) sin(y)
Elliott Hughesa0ee0782013-01-30 19:06:37 -080050 * = ---------------------------------
Elliott Hughes8cff2f92015-08-28 20:21:43 -070051 * cosh(x) cos(y) + I sinh(x) sin(y)
Elliott Hughesa0ee0782013-01-30 19:06:37 -080052 *
Elliott Hughes8cff2f92015-08-28 20:21:43 -070053 * cosh(x) sinh(x) / cos^2(y) + I tan(y)
Elliott Hughesa0ee0782013-01-30 19:06:37 -080054 * = -------------------------------------
55 * 1 + sinh^2(x) / cos^2(y)
56 *
Elliott Hughes8cff2f92015-08-28 20:21:43 -070057 * beta rho s + I t
Elliott Hughesa0ee0782013-01-30 19:06:37 -080058 * = ----------------
59 * 1 + beta s^2
60 *
61 * Modifications:
62 *
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.
66 */
67
68#include <sys/cdefs.h>
Elliott Hughesbac0ebb2021-01-26 14:17:20 -080069__FBSDID("$FreeBSD$");
Elliott Hughesa0ee0782013-01-30 19:06:37 -080070
71#include <complex.h>
72#include <math.h>
73
74#include "math_private.h"
75
76double complex
77ctanh(double complex z)
78{
79 double x, y;
80 double t, beta, s, rho, denom;
81 uint32_t hx, ix, lx;
82
83 x = creal(z);
84 y = cimag(z);
85
86 EXTRACT_WORDS(hx, lx, x);
87 ix = hx & 0x7fffffff;
88
89 /*
Elliott Hughes8cff2f92015-08-28 20:21:43 -070090 * ctanh(NaN +- I 0) = d(NaN) +- I 0
Elliott Hughesa0ee0782013-01-30 19:06:37 -080091 *
Elliott Hughes8cff2f92015-08-28 20:21:43 -070092 * ctanh(NaN + I y) = d(NaN,y) + I d(NaN,y) for y != 0
Elliott Hughesa0ee0782013-01-30 19:06:37 -080093 *
94 * The imaginary part has the sign of x*sin(2*y), but there's no
95 * special effort to get this right.
96 *
Elliott Hughes8cff2f92015-08-28 20:21:43 -070097 * ctanh(+-Inf +- I Inf) = +-1 +- I 0
Elliott Hughesa0ee0782013-01-30 19:06:37 -080098 *
Elliott Hughes8cff2f92015-08-28 20:21:43 -070099 * ctanh(+-Inf + I y) = +-1 + I 0 sin(2y) for y finite
Elliott Hughesa0ee0782013-01-30 19:06:37 -0800100 *
101 * The imaginary part of the sign is unspecified. This special
102 * case is only needed to avoid a spurious invalid exception when
103 * y is infinite.
104 */
105 if (ix >= 0x7ff00000) {
106 if ((ix & 0xfffff) | lx) /* x is NaN */
Elliott Hughesab528072018-07-24 00:01:52 +0000107 return (CMPLX(nan_mix(x, y),
108 y == 0 ? y : nan_mix(x, y)));
Elliott Hughesa0ee0782013-01-30 19:06:37 -0800109 SET_HIGH_WORD(x, hx - 0x40000000); /* x = copysign(1, x) */
Elliott Hughes8cff2f92015-08-28 20:21:43 -0700110 return (CMPLX(x, copysign(0, isinf(y) ? y : sin(y) * cos(y))));
Elliott Hughesa0ee0782013-01-30 19:06:37 -0800111 }
112
113 /*
Elliott Hughes99ef4472022-01-12 17:51:20 -0800114 * ctanh(+-0 + i NAN) = +-0 + i NaN
115 * ctanh(+-0 +- i Inf) = +-0 + i NaN
116 * ctanh(x + i NAN) = NaN + i NaN
117 * ctanh(x +- i Inf) = NaN + i NaN
Elliott Hughesa0ee0782013-01-30 19:06:37 -0800118 */
119 if (!isfinite(y))
Elliott Hughes99ef4472022-01-12 17:51:20 -0800120 return (CMPLX(x ? y - y : x, y - y));
Elliott Hughesa0ee0782013-01-30 19:06:37 -0800121
122 /*
Elliott Hughes8cff2f92015-08-28 20:21:43 -0700123 * ctanh(+-huge +- I y) ~= +-1 +- I 2sin(2y)/exp(2x), using the
Elliott Hughesa0ee0782013-01-30 19:06:37 -0800124 * approximation sinh^2(huge) ~= exp(2*huge) / 4.
125 * We use a modified formula to avoid spurious overflow.
126 */
Elliott Hughes8cff2f92015-08-28 20:21:43 -0700127 if (ix >= 0x40360000) { /* |x| >= 22 */
Elliott Hughesa0ee0782013-01-30 19:06:37 -0800128 double exp_mx = exp(-fabs(x));
Elliott Hughes8cff2f92015-08-28 20:21:43 -0700129 return (CMPLX(copysign(1, x),
Elliott Hughesa0ee0782013-01-30 19:06:37 -0800130 4 * sin(y) * cos(y) * exp_mx * exp_mx));
131 }
132
133 /* Kahan's algorithm */
134 t = tan(y);
135 beta = 1.0 + t * t; /* = 1 / cos^2(y) */
136 s = sinh(x);
137 rho = sqrt(1 + s * s); /* = cosh(x) */
138 denom = 1 + beta * s * s;
Elliott Hughes8cff2f92015-08-28 20:21:43 -0700139 return (CMPLX((beta * rho * s) / denom, t / denom));
Elliott Hughesa0ee0782013-01-30 19:06:37 -0800140}
141
142double complex
143ctan(double complex z)
144{
145
Elliott Hughes8cff2f92015-08-28 20:21:43 -0700146 /* ctan(z) = -I * ctanh(I * z) = I * conj(ctanh(I * conj(z))) */
147 z = ctanh(CMPLX(cimag(z), creal(z)));
148 return (CMPLX(cimag(z), creal(z)));
Elliott Hughesa0ee0782013-01-30 19:06:37 -0800149}