unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* inexact->exact on nan and inf
@ 2003-09-23 23:50 Kevin Ryde
  0 siblings, 0 replies; only message in thread
From: Kevin Ryde @ 2003-09-23 23:50 UTC (permalink / raw)


[-- Attachment #1: Type: text/plain, Size: 1433 bytes --]

        * numbers.c (scm_inexact_to_exact): Don't depend on what double->long
        cast gives for values bigger than a long, or for nan or inf.

        * tests/numbers.test (inexact->exact): New tests.

This is merely a defensive proposal, it actually comes out ok on my
i386 debian already, since casting double->long gives 0x80000000 or
0x7FFFFFFF for values out of range, which of course don't pass
SCM_FIXABLE.  But it doesn't seem wise to assume such values.

New code, for ease of contemplation,

      /* The values SCM_MOST_POSITIVE_FIXNUM+1 and SCM_MOST_NEGATIVE_FIXNUM
         are both powers of 2, so there's no rounding when making "double"
         values from them.  If plain SCM_MOST_POSITIVE_FIXNUM was used it
         could get rounded on a 64-bit machine, hence the "+1".

         The use of floor to force to an integer value ensures we don't
         depend on how a double->long cast will round or how mpz_set_d will
         round.  For reference, double->long probably follows the hardware
         rounding mode, whereas mpz_set_d truncates towards zero.  */

      double u = SCM_REAL_VALUE (z);
      if (xisinf (u) || xisnan (u))
        scm_num_overflow (s_scm_inexact_to_exact);
      u = floor (u + 0.5);
      if (u < (double) (SCM_MOST_POSITIVE_FIXNUM+1)
          && u >= (double) SCM_MOST_NEGATIVE_FIXNUM)
        return SCM_MAKINUM ((long) u);
      else
        return scm_i_dbl2big (u);



[-- Attachment #2: numbers.c.inexact-nan.diff --]
[-- Type: text/plain, Size: 1366 bytes --]

--- numbers.c.~1.200.~	1970-01-01 10:00:01.000000000 +1000
+++ numbers.c	2003-09-23 16:33:30.000000000 +1000
@@ -4337,14 +4337,25 @@
     return z;
   else if (SCM_REALP (z))
     {
-      double u = floor (SCM_REAL_VALUE (z) + 0.5);
-      long lu = (long) u;
-      if (SCM_FIXABLE (lu))
-	return SCM_MAKINUM (lu);
-      else if (!xisinf (u) && !xisnan (u))
-	return scm_i_dbl2big (u);
-      else
+      /* The values SCM_MOST_POSITIVE_FIXNUM+1 and SCM_MOST_NEGATIVE_FIXNUM
+         are both powers of 2, so there's no rounding when making "double"
+         values from them.  If plain SCM_MOST_POSITIVE_FIXNUM was used it
+         could get rounded on a 64-bit machine, hence the "+1".
+
+         The use of floor to force to an integer value ensures we don't
+         depend on how a double->long cast will round or how mpz_set_d will
+         round.  For reference, double->long probably follows the hardware
+         rounding mode, but mpz_set_d truncates towards zero.  */
+
+      double u = SCM_REAL_VALUE (z);
+      if (xisinf (u) || xisnan (u))
 	scm_num_overflow (s_scm_inexact_to_exact);
+      u = floor (u + 0.5);
+      if (u < (double) (SCM_MOST_POSITIVE_FIXNUM+1)
+          && u >= (double) SCM_MOST_NEGATIVE_FIXNUM)
+	return SCM_MAKINUM ((long) u);
+      else
+	return scm_i_dbl2big (u);
     }
   else
     SCM_WRONG_TYPE_ARG (1, z);

[-- Attachment #3: numbers.test.inexact.diff --]
[-- Type: text/plain, Size: 692 bytes --]

--- numbers.test.~1.32.~	1970-01-01 10:00:01.000000000 +1000
+++ numbers.test	2003-09-22 16:34:30.000000000 +1000
@@ -2015,6 +2015,24 @@
 ;;; inexact->exact
 ;;;
 
+(with-test-prefix "inexact->exact"
+  
+  (pass-if-exception exception:numerical-overflow "+inf"
+    (inexact->exact +.inf))
+  
+  (pass-if-exception exception:numerical-overflow "-inf"
+    (inexact->exact -.inf))
+  
+  (pass-if-exception exception:numerical-overflow "nan"
+    (inexact->exact +.nan))
+  
+  (with-test-prefix "2.0**i to exact and back"
+    (do ((i 0   (1+ i))
+	 (n 1.0 (* 2.0 n)))
+	((> i 100))
+      (pass-if (list i n)
+	(= n (inexact->exact (exact->inexact n)))))))
+
 ;;;
 ;;; integer-length
 ;;;

[-- Attachment #4: Type: text/plain, Size: 142 bytes --]

_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2003-09-23 23:50 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-09-23 23:50 inexact->exact on nan and inf Kevin Ryde

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).