1 /*	$OpenBSD: s_csqrt.c,v 1.6 2013/07/03 04:46:36 espie Exp $	*/
2 /*
3  * Copyright (c) 2008 Stephen L. Moshier <steve@moshier.net>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 /*							csqrt()
19  *
20  *	Complex square root
21  *
22  *
23  *
24  * SYNOPSIS:
25  *
26  * double complex csqrt();
27  * double complex z, w;
28  *
29  * w = csqrt (z);
30  *
31  *
32  *
33  * DESCRIPTION:
34  *
35  *
36  * If z = x + iy,  r = |z|, then
37  *
38  *                       1/2
39  * Re w  =  [ (r + x)/2 ]   ,
40  *
41  *                       1/2
42  * Im w  =  [ (r - x)/2 ]   .
43  *
44  * Cancellation error in r-x or r+x is avoided by using the
45  * identity  2 Re w Im w  =  y.
46  *
47  * Note that -w is also a square root of z.  The root chosen
48  * is always in the right half plane and Im w has the same sign as y.
49  *
50  *
51  *
52  * ACCURACY:
53  *
54  *                      Relative error:
55  * arithmetic   domain     # trials      peak         rms
56  *    DEC       -10,+10     25000       3.2e-17     9.6e-18
57  *    IEEE      -10,+10   1,000,000     2.9e-16     6.1e-17
58  *
59  */
60 
61 #include <complex.h>
62 #include <float.h>
63 #include <math.h>
64 
65 double complex
66 csqrt(double complex z)
67 {
68 	double complex w;
69 	double x, y, r, t, scale;
70 
71 	x = creal (z);
72 	y = cimag (z);
73 
74 	if (y == 0.0) {
75 		if (x == 0.0) {
76 			w = 0.0 + y * I;
77 		}
78 		else {
79 			r = fabs (x);
80 			r = sqrt (r);
81 			if (x < 0.0) {
82 				w = 0.0 + r * I;
83 			}
84 			else {
85 				w = r + y * I;
86 			}
87 		}
88 		return (w);
89 	}
90 	if (x == 0.0) {
91 		r = fabs (y);
92 		r = sqrt (0.5*r);
93 		if (y > 0)
94 			w = r + r * I;
95 		else
96 			w = r - r * I;
97 		return (w);
98 	}
99 	/* Rescale to avoid internal overflow or underflow.  */
100 	if ((fabs(x) > 4.0) || (fabs(y) > 4.0)) {
101 		x *= 0.25;
102 		y *= 0.25;
103 		scale = 2.0;
104 	}
105 	else {
106 		x *= 1.8014398509481984e16;  /* 2^54 */
107 		y *= 1.8014398509481984e16;
108 		scale = 7.450580596923828125e-9; /* 2^-27 */
109 #if 0
110 		x *= 4.0;
111 		y *= 4.0;
112 		scale = 0.5;
113 #endif
114 	}
115 	w = x + y * I;
116 	r = cabs(w);
117 	if (x > 0) {
118 		t = sqrt(0.5 * r + 0.5 * x);
119 		r = scale * fabs((0.5 * y) / t);
120 		t *= scale;
121 	}
122 	else {
123 		r = sqrt( 0.5 * r - 0.5 * x );
124 		t = scale * fabs( (0.5 * y) / r );
125 		r *= scale;
126 	}
127 	if (y < 0)
128 		w = t - r * I;
129 	else
130 		w = t + r * I;
131 	return (w);
132 }
133 
134 #if	LDBL_MANT_DIG == DBL_MANT_DIG
135 __strong_alias(csqrtl, csqrt);
136 #endif	/* LDBL_MANT_DIG == DBL_MANT_DIG */
137