changeset 16485:dea6693446fa

fmodl-ieee: Fix test failures. * lib/fmodl.c (fmodl): Treat Inf specially. * modules/fmodl (Depends-on): Add isinf.
author Bruno Haible <bruno@clisp.org>
date Mon, 27 Feb 2012 11:35:41 +0100
parents ba1e81c9042b
children 8b3ead70232c
files ChangeLog lib/fmodl.c modules/fmodl
diffstat 3 files changed, 45 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
 2012-02-27  Bruno Haible  <bruno@clisp.org>
 
+	fmodl-ieee: Fix test failures.
+	* lib/fmodl.c (fmodl): Treat Inf specially.
+	* modules/fmodl (Depends-on): Add isinf.
+
 	Tests for module 'fmodl-ieee'.
 	* modules/fmodl-ieee-tests: New file.
 	* tests/test-fmodl-ieee.c: New file.
--- a/lib/fmodl.c
+++ b/lib/fmodl.c
@@ -32,48 +32,53 @@
 long double
 fmodl (long double x, long double y)
 {
-  long double q = - truncl (x / y);
-  long double r = fmal (q, y, x); /* = x + q * y, computed in one step */
-  /* Correct possible rounding errors in the quotient x / y.  */
-  if (y >= 0)
+  if (isinf (y))
+    return x;
+  else
     {
-      if (x >= 0)
+      long double q = - truncl (x / y);
+      long double r = fmal (q, y, x); /* = x + q * y, computed in one step */
+      /* Correct possible rounding errors in the quotient x / y.  */
+      if (y >= 0)
         {
-          /* Expect 0 <= r < y.  */
-          if (r < 0)
-            q += 1, r = fmal (q, y, x);
-          else if (r >= y)
-            q -= 1, r = fmal (q, y, x);
+          if (x >= 0)
+            {
+              /* Expect 0 <= r < y.  */
+              if (r < 0)
+                q += 1, r = fmal (q, y, x);
+              else if (r >= y)
+                q -= 1, r = fmal (q, y, x);
+            }
+          else
+            {
+              /* Expect - y < r <= 0.  */
+              if (r > 0)
+                q -= 1, r = fmal (q, y, x);
+              else if (r <= - y)
+                q += 1, r = fmal (q, y, x);
+            }
         }
       else
         {
-          /* Expect - y < r <= 0.  */
-          if (r > 0)
-            q -= 1, r = fmal (q, y, x);
-          else if (r <= - y)
-            q += 1, r = fmal (q, y, x);
+          if (x >= 0)
+            {
+              /* Expect 0 <= r < - y.  */
+              if (r < 0)
+                q -= 1, r = fmal (q, y, x);
+              else if (r >= - y)
+                q += 1, r = fmal (q, y, x);
+            }
+          else
+            {
+              /* Expect y < r <= 0.  */
+              if (r > 0)
+                q += 1, r = fmal (q, y, x);
+              else if (r <= y)
+                q -= 1, r = fmal (q, y, x);
+            }
         }
+      return r;
     }
-  else
-    {
-      if (x >= 0)
-        {
-          /* Expect 0 <= r < - y.  */
-          if (r < 0)
-            q -= 1, r = fmal (q, y, x);
-          else if (r >= - y)
-            q += 1, r = fmal (q, y, x);
-        }
-      else
-        {
-          /* Expect y < r <= 0.  */
-          if (r > 0)
-            q += 1, r = fmal (q, y, x);
-          else if (r <= y)
-            q -= 1, r = fmal (q, y, x);
-        }
-    }
-  return r;
 }
 
 #endif
--- a/modules/fmodl
+++ b/modules/fmodl
@@ -9,6 +9,7 @@
 Depends-on:
 math
 fmod            [{ test $HAVE_FMODL = 0 || test $REPLACE_FMODL = 1; } && test $HAVE_SAME_LONG_DOUBLE_AS_DOUBLE = 1]
+isinf           [{ test $HAVE_FMODL = 0 || test $REPLACE_FMODL = 1; } && test $HAVE_SAME_LONG_DOUBLE_AS_DOUBLE = 0]
 truncl          [{ test $HAVE_FMODL = 0 || test $REPLACE_FMODL = 1; } && test $HAVE_SAME_LONG_DOUBLE_AS_DOUBLE = 0]
 fmal            [{ test $HAVE_FMODL = 0 || test $REPLACE_FMODL = 1; } && test $HAVE_SAME_LONG_DOUBLE_AS_DOUBLE = 0]