guile-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: real == frac


From: Kevin Ryde
Subject: Re: real == frac
Date: Tue, 17 Feb 2004 09:09:23 +1000
User-agent: Gnus/5.110002 (No Gnus v0.2) Emacs/21.3 (gnu/linux)

        * numbers.c (scm_num_eq_p): For real==frac, complex==frac, frac==real
        and frac==complex, make an exact comparison rather than converting
        with fraction2double.

I've added code under the complex cases because it was there already.
But I might have mentioned before that I thought all those could be
false immediately, if COMPLEXP always has a non-zero imaginary part.

--- numbers.c.~1.221.~  2004-01-07 07:54:59.000000000 +1000
+++ numbers.c   2004-02-12 11:58:18.000000000 +1000
@@ -2945,6 +2945,7 @@
 SCM
 scm_num_eq_p (SCM x, SCM y)
 {
+ again:
   if (SCM_INUMP (x))
     {
       long xx = SCM_INUM (x);
@@ -3019,7 +3020,15 @@
        return SCM_BOOL ((SCM_REAL_VALUE (x) == SCM_COMPLEX_REAL (y))
                         && (0.0 == SCM_COMPLEX_IMAG (y)));
       else if (SCM_FRACTIONP (y))
-       return SCM_BOOL (SCM_REAL_VALUE (x) == scm_i_fraction2double (y));
+        {
+          double  xx = SCM_REAL_VALUE (x);
+          if (xisnan (xx))
+            return SCM_BOOL_F;
+          if (xisinf (xx))
+            return SCM_BOOL (xx < 0.0);
+          x = scm_inexact_to_exact (x);  /* with x as frac or int */
+          goto again;
+        }
       else
        SCM_WTA_DISPATCH_2 (g_eq_p, x, y, SCM_ARGn, s_eq_p);
     }
@@ -3046,8 +3055,18 @@
        return SCM_BOOL ((SCM_COMPLEX_REAL (x) == SCM_COMPLEX_REAL (y))
                         && (SCM_COMPLEX_IMAG (x) == SCM_COMPLEX_IMAG (y)));
       else if (SCM_FRACTIONP (y))
-       return SCM_BOOL ((SCM_COMPLEX_REAL (x) == scm_i_fraction2double (y))
-                        && (SCM_COMPLEX_IMAG (x) == 0.0));
+        {
+          double  xx;
+          if (SCM_COMPLEX_IMAG (x) != 0.0)
+            return SCM_BOOL_F;
+          xx = SCM_COMPLEX_REAL (x);
+          if (xisnan (xx))
+            return SCM_BOOL_F;
+          if (xisinf (xx))
+            return SCM_BOOL (xx < 0.0);
+          x = scm_inexact_to_exact (x);  /* with x as frac or int */
+          goto again;
+        }
       else
        SCM_WTA_DISPATCH_2 (g_eq_p, x, y, SCM_ARGn, s_eq_p);
     }
@@ -3058,10 +3077,28 @@
       else if (SCM_BIGP (y))
        return SCM_BOOL_F;
       else if (SCM_REALP (y))
-       return SCM_BOOL (scm_i_fraction2double (x) == SCM_REAL_VALUE (y));
+        {
+          double yy = SCM_REAL_VALUE (y);
+          if (xisnan (yy))
+            return SCM_BOOL_F;
+          if (xisinf (yy))
+            return SCM_BOOL (0.0 < yy);
+          y = scm_inexact_to_exact (y);  /* with y as frac or int */
+          goto again;
+        }
       else if (SCM_COMPLEXP (y))
-       return SCM_BOOL ((scm_i_fraction2double (x) == SCM_COMPLEX_REAL (y))
-                        && (0.0 == SCM_COMPLEX_IMAG (y)));
+        {
+          double yy;
+          if (SCM_COMPLEX_IMAG (y) != 0.0)
+            return SCM_BOOL_F;
+          yy = SCM_COMPLEX_REAL (y);
+          if (xisnan (yy))
+            return SCM_BOOL_F;
+          if (xisinf (yy))
+            return SCM_BOOL (0.0 < yy);
+          y = scm_inexact_to_exact (y);  /* with y as frac or int */
+          goto again;
+        }
       else if (SCM_FRACTIONP (y))
        return scm_i_fraction_equalp (x, y);
       else
--- numbers.test.~1.40.~        2003-12-09 15:06:40.000000000 +1000
+++ numbers.test        2004-02-17 09:07:42.000000000 +1000
@@ -1308,7 +1308,25 @@
   ;; in gmp prior to 4.2, mpz_cmp_d ended up treating NaN as 3*2^1023, make
   ;; sure we've avoided that
   (pass-if (not (= (ash 3 1023) +nan.0)))
-  (pass-if (not (= +nan.0 (ash 3 1023)))))
+  (pass-if (not (= +nan.0 (ash 3 1023))))
+
+  (pass-if (= 1/2 0.5))
+  (pass-if (not (= 1/3 0.333333333333333333333333333333333)))
+  (pass-if (not (= 2/3 0.5)))
+  (pass-if (not (= 0.5 (+ 1/2 (/ 1 (ash 1 1000))))))
+
+  (pass-if (= 1/2 0.5+0i))
+  (pass-if (not (= 0.333333333333333333333333333333333 1/3)))
+  (pass-if (not (= 2/3 0.5+0i)))
+  (pass-if (not (= 1/2 0+0.5i)))
+
+  (pass-if (= 0.5 1/2))
+  (pass-if (not (= 0.5 2/3)))
+  (pass-if (not (= (+ 1/2 (/ 1 (ash 1 1000))) 0.5)))
+
+  (pass-if (= 0.5+0i 1/2))
+  (pass-if (not (= 0.5+0i 2/3)))
+  (pass-if (not (= 0+0.5i 1/2))))
 
 ;;;
 ;;; <

reply via email to

[Prev in Thread] Current Thread [Next in Thread]