]>
Commit | Line | Data |
---|---|---|
63551311 | 1 | /* Complex square root of double value. |
72c7a71d | 2 | Copyright (C) 1997, 1998, 2005 Free Software Foundation, Inc. |
63551311 UD |
3 | This file is part of the GNU C Library. |
4 | Based on an algorithm by Stephen L. Moshier <moshier@world.std.com>. | |
5 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997. | |
6 | ||
7 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
8 | modify it under the terms of the GNU Lesser General Public |
9 | License as published by the Free Software Foundation; either | |
10 | version 2.1 of the License, or (at your option) any later version. | |
63551311 UD |
11 | |
12 | The GNU C Library is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 15 | Lesser General Public License for more details. |
63551311 | 16 | |
41bdb6e2 AJ |
17 | You should have received a copy of the GNU Lesser General Public |
18 | License along with the GNU C Library; if not, write to the Free | |
19 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA | |
20 | 02111-1307 USA. */ | |
63551311 UD |
21 | |
22 | #include <complex.h> | |
23 | #include <math.h> | |
24 | ||
25 | #include "math_private.h" | |
26 | ||
27 | ||
28 | __complex__ double | |
29 | __csqrt (__complex__ double x) | |
30 | { | |
31 | __complex__ double res; | |
32 | int rcls = fpclassify (__real__ x); | |
33 | int icls = fpclassify (__imag__ x); | |
34 | ||
35 | if (rcls <= FP_INFINITE || icls <= FP_INFINITE) | |
36 | { | |
37 | if (icls == FP_INFINITE) | |
38 | { | |
39 | __real__ res = HUGE_VAL; | |
40 | __imag__ res = __imag__ x; | |
41 | } | |
42 | else if (rcls == FP_INFINITE) | |
43 | { | |
44 | if (__real__ x < 0.0) | |
45 | { | |
46 | __real__ res = icls == FP_NAN ? __nan ("") : 0; | |
47 | __imag__ res = __copysign (HUGE_VAL, __imag__ x); | |
48 | } | |
49 | else | |
50 | { | |
51 | __real__ res = __real__ x; | |
52 | __imag__ res = (icls == FP_NAN | |
53 | ? __nan ("") : __copysign (0.0, __imag__ x)); | |
54 | } | |
55 | } | |
56 | else | |
57 | { | |
58 | __real__ res = __nan (""); | |
59 | __imag__ res = __nan (""); | |
60 | } | |
61 | } | |
62 | else | |
63 | { | |
64 | if (icls == FP_ZERO) | |
65 | { | |
66 | if (__real__ x < 0.0) | |
67 | { | |
68 | __real__ res = 0.0; | |
69 | __imag__ res = __copysign (__ieee754_sqrt (-__real__ x), | |
70 | __imag__ x); | |
71 | } | |
72 | else | |
73 | { | |
74 | __real__ res = fabs (__ieee754_sqrt (__real__ x)); | |
75 | __imag__ res = __copysign (0.0, __imag__ x); | |
76 | } | |
77 | } | |
78 | else if (rcls == FP_ZERO) | |
79 | { | |
80 | double r = __ieee754_sqrt (0.5 * fabs (__imag__ x)); | |
81 | ||
72c7a71d UD |
82 | __real__ res = r; |
83 | __imag__ res = __copysign (r, __imag__ x); | |
63551311 UD |
84 | } |
85 | else | |
86 | { | |
ec986e23 | 87 | double d, r, s; |
63551311 | 88 | |
ec986e23 UD |
89 | d = __ieee754_hypot (__real__ x, __imag__ x); |
90 | /* Use the identity 2 Re res Im res = Im x | |
91 | to avoid cancellation error in d +/- Re x. */ | |
92 | if (__real__ x > 0) | |
93 | { | |
94 | r = __ieee754_sqrt (0.5 * d + 0.5 * __real__ x); | |
95 | s = (0.5 * __imag__ x) / r; | |
96 | } | |
63551311 | 97 | else |
ec986e23 UD |
98 | { |
99 | s = __ieee754_sqrt (0.5 * d - 0.5 * __real__ x); | |
786a5421 | 100 | r = fabs ((0.5 * __imag__ x) / s); |
ec986e23 | 101 | } |
63551311 | 102 | |
ec986e23 UD |
103 | __real__ res = r; |
104 | __imag__ res = __copysign (s, __imag__ x); | |
63551311 UD |
105 | } |
106 | } | |
107 | ||
108 | return res; | |
109 | } | |
110 | weak_alias (__csqrt, csqrt) | |
111 | #ifdef NO_LONG_DOUBLE | |
112 | strong_alias (__csqrt, __csqrtl) | |
113 | weak_alias (__csqrt, csqrtl) | |
114 | #endif |