]> git.ipfire.org Git - thirdparty/glibc.git/blame - math/s_catanh_template.c
Improve the accuracy of tgamma (BZ #26983)
[thirdparty/glibc.git] / math / s_catanh_template.c
CommitLineData
d5602ceb 1/* Return arc hyperbolic tangent for a complex float type.
2b778ceb 2 Copyright (C) 1997-2021 Free Software Foundation, Inc.
f6d3a72e
PM
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
5a82c748 18 <https://www.gnu.org/licenses/>. */
f6d3a72e
PM
19
20#include <complex.h>
21#include <math.h>
22#include <math_private.h>
8f5b00d3 23#include <math-underflow.h>
f6d3a72e
PM
24#include <float.h>
25
d5602ceb
PM
26CFLOAT
27M_DECL_FUNC (__catanh) (CFLOAT x)
f6d3a72e 28{
d5602ceb 29 CFLOAT res;
f6d3a72e
PM
30 int rcls = fpclassify (__real__ x);
31 int icls = fpclassify (__imag__ x);
32
33 if (__glibc_unlikely (rcls <= FP_INFINITE || icls <= FP_INFINITE))
34 {
35 if (icls == FP_INFINITE)
36 {
d5602ceb
PM
37 __real__ res = M_COPYSIGN (0, __real__ x);
38 __imag__ res = M_COPYSIGN (M_MLIT (M_PI_2), __imag__ x);
f6d3a72e
PM
39 }
40 else if (rcls == FP_INFINITE || rcls == FP_ZERO)
41 {
d5602ceb 42 __real__ res = M_COPYSIGN (0, __real__ x);
f6d3a72e 43 if (icls >= FP_ZERO)
d5602ceb 44 __imag__ res = M_COPYSIGN (M_MLIT (M_PI_2), __imag__ x);
f6d3a72e 45 else
d5602ceb 46 __imag__ res = M_NAN;
f6d3a72e
PM
47 }
48 else
49 {
d5602ceb
PM
50 __real__ res = M_NAN;
51 __imag__ res = M_NAN;
f6d3a72e
PM
52 }
53 }
54 else if (__glibc_unlikely (rcls == FP_ZERO && icls == FP_ZERO))
55 {
56 res = x;
57 }
58 else
59 {
d5602ceb
PM
60 if (M_FABS (__real__ x) >= 16 / M_EPSILON
61 || M_FABS (__imag__ x) >= 16 / M_EPSILON)
f6d3a72e 62 {
d5602ceb
PM
63 __imag__ res = M_COPYSIGN (M_MLIT (M_PI_2), __imag__ x);
64 if (M_FABS (__imag__ x) <= 1)
65 __real__ res = 1 / __real__ x;
66 else if (M_FABS (__real__ x) <= 1)
f6d3a72e
PM
67 __real__ res = __real__ x / __imag__ x / __imag__ x;
68 else
69 {
d5602ceb
PM
70 FLOAT h = M_HYPOT (__real__ x / 2, __imag__ x / 2);
71 __real__ res = __real__ x / h / h / 4;
f6d3a72e
PM
72 }
73 }
74 else
75 {
d5602ceb
PM
76 if (M_FABS (__real__ x) == 1
77 && M_FABS (__imag__ x) < M_EPSILON * M_EPSILON)
78 __real__ res = (M_COPYSIGN (M_LIT (0.5), __real__ x)
79 * ((FLOAT) M_MLIT (M_LN2)
80 - M_LOG (M_FABS (__imag__ x))));
f6d3a72e
PM
81 else
82 {
d5602ceb
PM
83 FLOAT i2 = 0;
84 if (M_FABS (__imag__ x) >= M_EPSILON * M_EPSILON)
f6d3a72e
PM
85 i2 = __imag__ x * __imag__ x;
86
d5602ceb 87 FLOAT num = 1 + __real__ x;
f6d3a72e
PM
88 num = i2 + num * num;
89
d5602ceb 90 FLOAT den = 1 - __real__ x;
f6d3a72e
PM
91 den = i2 + den * den;
92
d5602ceb
PM
93 FLOAT f = num / den;
94 if (f < M_LIT (0.5))
95 __real__ res = M_LIT (0.25) * M_LOG (f);
f6d3a72e
PM
96 else
97 {
d5602ceb
PM
98 num = 4 * __real__ x;
99 __real__ res = M_LIT (0.25) * M_LOG1P (num / den);
f6d3a72e
PM
100 }
101 }
102
d5602ceb 103 FLOAT absx, absy, den;
f6d3a72e 104
d5602ceb
PM
105 absx = M_FABS (__real__ x);
106 absy = M_FABS (__imag__ x);
f6d3a72e
PM
107 if (absx < absy)
108 {
d5602ceb 109 FLOAT t = absx;
f6d3a72e
PM
110 absx = absy;
111 absy = t;
112 }
113
d5602ceb 114 if (absy < M_EPSILON / 2)
f6d3a72e 115 {
d5602ceb
PM
116 den = (1 - absx) * (1 + absx);
117 if (den == 0)
118 den = 0;
f6d3a72e 119 }
d5602ceb
PM
120 else if (absx >= 1)
121 den = (1 - absx) * (1 + absx) - absy * absy;
122 else if (absx >= M_LIT (0.75) || absy >= M_LIT (0.5))
123 den = -M_SUF (__x2y2m1) (absx, absy);
f6d3a72e 124 else
d5602ceb 125 den = (1 - absx) * (1 + absx) - absy * absy;
f6d3a72e 126
d5602ceb 127 __imag__ res = M_LIT (0.5) * M_ATAN2 (2 * __imag__ x, den);
f6d3a72e
PM
128 }
129
130 math_check_force_underflow_complex (res);
131 }
132
133 return res;
134}
d5602ceb
PM
135
136declare_mgen_alias (__catanh, catanh)