]>
Commit | Line | Data |
---|---|---|
1dbc54f6 | 1 | /* Compute complex natural logarithm. |
2b778ceb | 2 | Copyright (C) 1997-2021 Free Software Foundation, Inc. |
1dbc54f6 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/>. */ |
1dbc54f6 PM |
19 | |
20 | #include <complex.h> | |
21 | #include <math.h> | |
22 | #include <math_private.h> | |
8f5b00d3 | 23 | #include <math-underflow.h> |
1dbc54f6 PM |
24 | #include <float.h> |
25 | ||
feb62dda PM |
26 | CFLOAT |
27 | M_DECL_FUNC (__clog) (CFLOAT x) | |
1dbc54f6 | 28 | { |
feb62dda | 29 | CFLOAT result; |
1dbc54f6 PM |
30 | int rcls = fpclassify (__real__ x); |
31 | int icls = fpclassify (__imag__ x); | |
32 | ||
33 | if (__glibc_unlikely (rcls == FP_ZERO && icls == FP_ZERO)) | |
34 | { | |
35 | /* Real and imaginary part are 0.0. */ | |
feb62dda PM |
36 | __imag__ result = signbit (__real__ x) ? (FLOAT) M_MLIT (M_PI) : 0; |
37 | __imag__ result = M_COPYSIGN (__imag__ result, __imag__ x); | |
1dbc54f6 | 38 | /* Yes, the following line raises an exception. */ |
feb62dda | 39 | __real__ result = -1 / M_FABS (__real__ x); |
1dbc54f6 PM |
40 | } |
41 | else if (__glibc_likely (rcls != FP_NAN && icls != FP_NAN)) | |
42 | { | |
43 | /* Neither real nor imaginary part is NaN. */ | |
feb62dda | 44 | FLOAT absx = M_FABS (__real__ x), absy = M_FABS (__imag__ x); |
1dbc54f6 PM |
45 | int scale = 0; |
46 | ||
47 | if (absx < absy) | |
48 | { | |
feb62dda | 49 | FLOAT t = absx; |
1dbc54f6 PM |
50 | absx = absy; |
51 | absy = t; | |
52 | } | |
53 | ||
feb62dda | 54 | if (absx > M_MAX / 2) |
1dbc54f6 PM |
55 | { |
56 | scale = -1; | |
feb62dda PM |
57 | absx = M_SCALBN (absx, scale); |
58 | absy = (absy >= M_MIN * 2 ? M_SCALBN (absy, scale) : 0); | |
1dbc54f6 | 59 | } |
feb62dda | 60 | else if (absx < M_MIN && absy < M_MIN) |
1dbc54f6 | 61 | { |
feb62dda PM |
62 | scale = M_MANT_DIG; |
63 | absx = M_SCALBN (absx, scale); | |
64 | absy = M_SCALBN (absy, scale); | |
1dbc54f6 PM |
65 | } |
66 | ||
feb62dda | 67 | if (absx == 1 && scale == 0) |
1dbc54f6 | 68 | { |
feb62dda | 69 | __real__ result = M_LOG1P (absy * absy) / 2; |
1dbc54f6 PM |
70 | math_check_force_underflow_nonneg (__real__ result); |
71 | } | |
feb62dda | 72 | else if (absx > 1 && absx < 2 && absy < 1 && scale == 0) |
1dbc54f6 | 73 | { |
feb62dda PM |
74 | FLOAT d2m1 = (absx - 1) * (absx + 1); |
75 | if (absy >= M_EPSILON) | |
1dbc54f6 | 76 | d2m1 += absy * absy; |
feb62dda | 77 | __real__ result = M_LOG1P (d2m1) / 2; |
1dbc54f6 | 78 | } |
feb62dda PM |
79 | else if (absx < 1 |
80 | && absx >= M_LIT (0.5) | |
81 | && absy < M_EPSILON / 2 | |
1dbc54f6 PM |
82 | && scale == 0) |
83 | { | |
feb62dda PM |
84 | FLOAT d2m1 = (absx - 1) * (absx + 1); |
85 | __real__ result = M_LOG1P (d2m1) / 2; | |
1dbc54f6 | 86 | } |
feb62dda PM |
87 | else if (absx < 1 |
88 | && absx >= M_LIT (0.5) | |
1dbc54f6 | 89 | && scale == 0 |
feb62dda | 90 | && absx * absx + absy * absy >= M_LIT (0.5)) |
1dbc54f6 | 91 | { |
feb62dda PM |
92 | FLOAT d2m1 = M_SUF (__x2y2m1) (absx, absy); |
93 | __real__ result = M_LOG1P (d2m1) / 2; | |
1dbc54f6 PM |
94 | } |
95 | else | |
96 | { | |
feb62dda PM |
97 | FLOAT d = M_HYPOT (absx, absy); |
98 | __real__ result = M_LOG (d) - scale * (FLOAT) M_MLIT (M_LN2); | |
1dbc54f6 PM |
99 | } |
100 | ||
feb62dda | 101 | __imag__ result = M_ATAN2 (__imag__ x, __real__ x); |
1dbc54f6 PM |
102 | } |
103 | else | |
104 | { | |
feb62dda | 105 | __imag__ result = M_NAN; |
1dbc54f6 PM |
106 | if (rcls == FP_INFINITE || icls == FP_INFINITE) |
107 | /* Real or imaginary part is infinite. */ | |
feb62dda | 108 | __real__ result = M_HUGE_VAL; |
1dbc54f6 | 109 | else |
feb62dda | 110 | __real__ result = M_NAN; |
1dbc54f6 PM |
111 | } |
112 | ||
113 | return result; | |
114 | } | |
feb62dda PM |
115 | |
116 | declare_mgen_alias (__clog, clog) |