about summary refs log tree commit diff
path: root/sysdeps/libm-ieee754/s_csqrt.c
diff options
context:
space:
mode:
Diffstat (limited to 'sysdeps/libm-ieee754/s_csqrt.c')
-rw-r--r--sysdeps/libm-ieee754/s_csqrt.c31
1 files changed, 17 insertions, 14 deletions
diff --git a/sysdeps/libm-ieee754/s_csqrt.c b/sysdeps/libm-ieee754/s_csqrt.c
index cda23c702d..ae106bdad8 100644
--- a/sysdeps/libm-ieee754/s_csqrt.c
+++ b/sysdeps/libm-ieee754/s_csqrt.c
@@ -1,5 +1,5 @@
 /* Complex square root of double value.
-   Copyright (C) 1997 Free Software Foundation, Inc.
+   Copyright (C) 1997, 1998 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Based on an algorithm by Stephen L. Moshier <moshier@world.std.com>.
    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
@@ -84,22 +84,25 @@ __csqrt (__complex__ double x)
 	}
       else
 	{
-#if 0 /* FIXME: this is broken. */
-	  __complex__ double q;
-	  double t, r;
+#if 0
+	  double d, r, s;
 
-	  if (fabs (__imag__ x) < 2.0e-4 * fabs (__real__ x))
-	    t = 0.25 * __imag__ x * (__imag__ x / __real__ x);
+	  d = __ieee754_hypot (__real__ x, __imag__ x);
+	  /* Use the identity   2  Re res  Im res = Im x
+	     to avoid cancellation error in  d +/- Re x.  */
+	  if (__real__ x > 0)
+	    {
+	      r = __ieee754_sqrt (0.5 * d + 0.5 * __real__ x);
+	      s = (0.5 * __imag__ x) / r;
+	    }
 	  else
-	    t = 0.5 * (__ieee754_hypot (__real__ x, __imag__ x) - __real__ x);
-
-	  r = __ieee754_sqrt (t);
-
-	  __real__ q = __imag__ x / (2.0 * r);
-	  __imag__ q = r;
+	    {
+	      s = __ieee754_sqrt (0.5 * d - 0.5 * __real__ x);
+	      r = (0.5 * __imag__ x) / s;
+	    }
 
-	  /* Heron iteration in complex arithmetic.  */
-	  res = 0.5 * (q + q / x);
+	  __real__ res = r;
+	  __imag__ res = __copysign (s, __imag__ x);
 #else
 	  double d, imag;