1 /* Complex square root of long double value.
2 Copyright (C) 1997, 1998 Free Software Foundation, Inc.
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.
7 The GNU C Library is free software; you can redistribute it and/or
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.
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
15 Lesser General Public License for more details.
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
25 #include "math_private.h"
28 __complex__
long double
29 __csqrtl (__complex__
long double x
)
31 __complex__
long double res
;
32 int rcls
= fpclassify (__real__ x
);
33 int icls
= fpclassify (__imag__ x
);
35 if (rcls
<= FP_INFINITE
|| icls
<= FP_INFINITE
)
37 if (icls
== FP_INFINITE
)
39 __real__ res
= HUGE_VALL
;
40 __imag__ res
= __imag__ x
;
42 else if (rcls
== FP_INFINITE
)
46 __real__ res
= icls
== FP_NAN
? __nanl ("") : 0;
47 __imag__ res
= __copysignl (HUGE_VALL
, __imag__ x
);
51 __real__ res
= __real__ x
;
52 __imag__ res
= (icls
== FP_NAN
53 ? __nanl ("") : __copysignl (0.0, __imag__ x
));
58 __real__ res
= __nanl ("");
59 __imag__ res
= __nanl ("");
69 __imag__ res
= __copysignl (__ieee754_sqrtl (-__real__ x
),
74 __real__ res
= fabsl (__ieee754_sqrtl (__real__ x
));
75 __imag__ res
= __copysignl (0.0, __imag__ x
);
78 else if (rcls
== FP_ZERO
)
80 long double r
= __ieee754_sqrtl (0.5 * fabsl (__imag__ x
));
82 __real__ res
= __copysignl (r
, __imag__ x
);
89 d
= __ieee754_hypotl (__real__ x
, __imag__ x
);
90 /* Use the identity 2 Re res Im res = Im x
91 to avoid cancellation error in d +/- Re x. */
94 r
= __ieee754_sqrtl (0.5L * d
+ 0.5L * __real__ x
);
95 s
= (0.5L * __imag__ x
) / r
;
99 s
= __ieee754_sqrtl (0.5L * d
- 0.5L * __real__ x
);
100 r
= fabsl ((0.5L * __imag__ x
) / s
);
104 __imag__ res
= __copysignl (s
, __imag__ x
);
110 weak_alias (__csqrtl
, csqrtl
)