about summary refs log tree commit diff
path: root/math/s_catanl.c
diff options
context:
space:
mode:
authorJoseph Myers <joseph@codesourcery.com>2013-04-27 14:56:34 +0000
committerJoseph Myers <joseph@codesourcery.com>2013-04-27 14:57:41 +0000
commit5b4217d71f592287bc77d80e08a20873bce86756 (patch)
tree6c0cb83177b4bee4da605e7e3c6b91f798e2204b /math/s_catanl.c
parentd5ba74f76461d2eaca738413cd49bf28bebff2f1 (diff)
downloadglibc-5b4217d71f592287bc77d80e08a20873bce86756.tar.gz
glibc-5b4217d71f592287bc77d80e08a20873bce86756.tar.xz
glibc-5b4217d71f592287bc77d80e08a20873bce86756.zip
Fix catan, catanh spurious overflows (bug 15409).
Diffstat (limited to 'math/s_catanl.c')
-rw-r--r--math/s_catanl.c55
1 files changed, 40 insertions, 15 deletions
diff --git a/math/s_catanl.c b/math/s_catanl.c
index 2440e6d151..17ab62d340 100644
--- a/math/s_catanl.c
+++ b/math/s_catanl.c
@@ -22,6 +22,13 @@
 #include <math_private.h>
 #include <float.h>
 
+/* To avoid spurious overflows, use this definition to treat IBM long
+   double as approximating an IEEE-style format.  */
+#if LDBL_MANT_DIG == 106
+# undef LDBL_EPSILON
+# define LDBL_EPSILON 0x1p-106L
+#endif
+
 __complex__ long double
 __catanl (__complex__ long double x)
 {
@@ -61,27 +68,45 @@ __catanl (__complex__ long double x)
     }
   else
     {
-      long double r2, num, den, f;
+      if (fabsl (__real__ x) >= 16.0L / LDBL_EPSILON
+	  || fabsl (__imag__ x) >= 16.0L / LDBL_EPSILON)
+	{
+	  __real__ res = __copysignl (M_PI_2l, __real__ x);
+	  if (fabsl (__real__ x) <= 1.0L)
+	    __imag__ res = 1.0L / __imag__ x;
+	  else if (fabsl (__imag__ x) <= 1.0L)
+	    __imag__ res = __imag__ x / __real__ x / __real__ x;
+	  else
+	    {
+	      long double h = __ieee754_hypotl (__real__ x / 2.0L,
+						__imag__ x / 2.0L);
+	      __imag__ res = __imag__ x / h / h / 4.0L;
+	    }
+	}
+      else
+	{
+	  long double r2, num, den, f;
 
-      r2 = __real__ x * __real__ x;
+	  r2 = __real__ x * __real__ x;
 
-      den = 1 - r2 - __imag__ x * __imag__ x;
+	  den = 1 - r2 - __imag__ x * __imag__ x;
 
-      __real__ res = 0.5L * __ieee754_atan2l (2.0L * __real__ x, den);
+	  __real__ res = 0.5L * __ieee754_atan2l (2.0L * __real__ x, den);
 
-      num = __imag__ x + 1.0L;
-      num = r2 + num * num;
+	  num = __imag__ x + 1.0L;
+	  num = r2 + num * num;
 
-      den = __imag__ x - 1.0L;
-      den = r2 + den * den;
+	  den = __imag__ x - 1.0L;
+	  den = r2 + den * den;
 
-      f = num / den;
-      if (f < 0.5L)
-	__imag__ res = 0.25L * __ieee754_logl (f);
-      else
-	{
-	  num = 4.0L * __imag__ x;
-	  __imag__ res = 0.25L * __log1pl (num / den);
+	  f = num / den;
+	  if (f < 0.5L)
+	    __imag__ res = 0.25L * __ieee754_logl (f);
+	  else
+	    {
+	      num = 4.0L * __imag__ x;
+	      __imag__ res = 0.25L * __log1pl (num / den);
+	    }
 	}
 
       if (fabsl (__real__ res) < LDBL_MIN)