changeset 16945:d6cd00b2e214

(isnan): Define even if LISP_FLOAT_TYPE is not defined, since fmod might need it. (fmod): Ensure that the magnitude of the result does not exceed that of the divisor, and that the sign of the result does not disagree with that of the dividend. This does not yield a particularly accurate result, but at least it will be in the range promised by fmod.
author Paul Eggert <eggert@twinsun.com>
date Tue, 28 Jan 1997 04:51:45 +0000
parents 54108c7f46e6
children 0c76a3a4b4d6
files src/data.c
diffstat 1 files changed, 15 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/data.c	Mon Jan 27 22:55:04 1997 +0000
+++ b/src/data.c	Tue Jan 28 04:51:45 1997 +0000
@@ -2007,11 +2007,11 @@
   return val;
 }
 
-#ifdef LISP_FLOAT_TYPE
-
 #undef isnan
 #define isnan(x) ((x) != (x))
 
+#ifdef LISP_FLOAT_TYPE
+
 Lisp_Object
 float_arith_driver (accum, argnum, code, nargs, args)
      double accum;
@@ -2141,9 +2141,21 @@
 fmod (f1, f2)
      double f1, f2;
 {
+  double r = f1;
+
   if (f2 < 0.0)
     f2 = -f2;
-  return (f1 - f2 * floor (f1/f2));
+
+  /* If the magnitude of the result exceeds that of the divisor, or
+     the sign of the result does not agree with that of the dividend,
+     iterate with the reduced value.  This does not yield a
+     particularly accurate result, but at least it will be in the
+     range promised by fmod.  */
+  do
+    r -= f2 * floor (r / f2);
+  while (f2 <= (r < 0 ? -r : r) || ((r < 0) != (f1 < 0) && ! isnan (r)));
+
+  return r;
 }
 #endif /* ! HAVE_FMOD */