]>
Commit | Line | Data |
---|---|---|
d5602ceb | 1 | /* Return arc tangent of complex float type. |
04277e02 | 2 | Copyright (C) 1997-2019 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 |
26 | CFLOAT |
27 | M_DECL_FUNC (__catan) (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 (rcls == FP_INFINITE) | |
36 | { | |
d5602ceb PM |
37 | __real__ res = M_COPYSIGN (M_MLIT (M_PI_2), __real__ x); |
38 | __imag__ res = M_COPYSIGN (0, __imag__ x); | |
f6d3a72e PM |
39 | } |
40 | else if (icls == FP_INFINITE) | |
41 | { | |
42 | if (rcls >= FP_ZERO) | |
d5602ceb | 43 | __real__ res = M_COPYSIGN (M_MLIT (M_PI_2), __real__ x); |
f6d3a72e | 44 | else |
d5602ceb PM |
45 | __real__ res = M_NAN; |
46 | __imag__ res = M_COPYSIGN (0, __imag__ x); | |
f6d3a72e PM |
47 | } |
48 | else if (icls == FP_ZERO || icls == FP_INFINITE) | |
49 | { | |
d5602ceb PM |
50 | __real__ res = M_NAN; |
51 | __imag__ res = M_COPYSIGN (0, __imag__ x); | |
f6d3a72e PM |
52 | } |
53 | else | |
54 | { | |
d5602ceb PM |
55 | __real__ res = M_NAN; |
56 | __imag__ res = M_NAN; | |
f6d3a72e PM |
57 | } |
58 | } | |
59 | else if (__glibc_unlikely (rcls == FP_ZERO && icls == FP_ZERO)) | |
60 | { | |
61 | res = x; | |
62 | } | |
63 | else | |
64 | { | |
d5602ceb PM |
65 | if (M_FABS (__real__ x) >= 16 / M_EPSILON |
66 | || M_FABS (__imag__ x) >= 16 / M_EPSILON) | |
f6d3a72e | 67 | { |
d5602ceb PM |
68 | __real__ res = M_COPYSIGN (M_MLIT (M_PI_2), __real__ x); |
69 | if (M_FABS (__real__ x) <= 1) | |
70 | __imag__ res = 1 / __imag__ x; | |
71 | else if (M_FABS (__imag__ x) <= 1) | |
f6d3a72e PM |
72 | __imag__ res = __imag__ x / __real__ x / __real__ x; |
73 | else | |
74 | { | |
d5602ceb PM |
75 | FLOAT h = M_HYPOT (__real__ x / 2, __imag__ x / 2); |
76 | __imag__ res = __imag__ x / h / h / 4; | |
f6d3a72e PM |
77 | } |
78 | } | |
79 | else | |
80 | { | |
d5602ceb | 81 | FLOAT den, absx, absy; |
f6d3a72e | 82 | |
d5602ceb PM |
83 | absx = M_FABS (__real__ x); |
84 | absy = M_FABS (__imag__ x); | |
f6d3a72e PM |
85 | if (absx < absy) |
86 | { | |
d5602ceb | 87 | FLOAT t = absx; |
f6d3a72e PM |
88 | absx = absy; |
89 | absy = t; | |
90 | } | |
91 | ||
d5602ceb | 92 | if (absy < M_EPSILON / 2) |
f6d3a72e | 93 | { |
d5602ceb PM |
94 | den = (1 - absx) * (1 + absx); |
95 | if (den == 0) | |
96 | den = 0; | |
f6d3a72e | 97 | } |
d5602ceb PM |
98 | else if (absx >= 1) |
99 | den = (1 - absx) * (1 + absx) - absy * absy; | |
100 | else if (absx >= M_LIT (0.75) || absy >= M_LIT (0.5)) | |
101 | den = -M_SUF (__x2y2m1) (absx, absy); | |
f6d3a72e | 102 | else |
d5602ceb | 103 | den = (1 - absx) * (1 + absx) - absy * absy; |
f6d3a72e | 104 | |
d5602ceb | 105 | __real__ res = M_LIT (0.5) * M_ATAN2 (2 * __real__ x, den); |
f6d3a72e | 106 | |
d5602ceb PM |
107 | if (M_FABS (__imag__ x) == 1 |
108 | && M_FABS (__real__ x) < M_EPSILON * M_EPSILON) | |
109 | __imag__ res = (M_COPYSIGN (M_LIT (0.5), __imag__ x) | |
110 | * ((FLOAT) M_MLIT (M_LN2) | |
111 | - M_LOG (M_FABS (__real__ x)))); | |
f6d3a72e PM |
112 | else |
113 | { | |
d5602ceb | 114 | FLOAT r2 = 0, num, f; |
f6d3a72e | 115 | |
d5602ceb | 116 | if (M_FABS (__real__ x) >= M_EPSILON * M_EPSILON) |
f6d3a72e PM |
117 | r2 = __real__ x * __real__ x; |
118 | ||
d5602ceb | 119 | num = __imag__ x + 1; |
f6d3a72e PM |
120 | num = r2 + num * num; |
121 | ||
d5602ceb | 122 | den = __imag__ x - 1; |
f6d3a72e PM |
123 | den = r2 + den * den; |
124 | ||
125 | f = num / den; | |
d5602ceb PM |
126 | if (f < M_LIT (0.5)) |
127 | __imag__ res = M_LIT (0.25) * M_LOG (f); | |
f6d3a72e PM |
128 | else |
129 | { | |
d5602ceb PM |
130 | num = 4 * __imag__ x; |
131 | __imag__ res = M_LIT (0.25) * M_LOG1P (num / den); | |
f6d3a72e PM |
132 | } |
133 | } | |
134 | } | |
135 | ||
136 | math_check_force_underflow_complex (res); | |
137 | } | |
138 | ||
139 | return res; | |
140 | } | |
d5602ceb PM |
141 | |
142 | declare_mgen_alias (__catan, catan) |