unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Kevin Ryde <user42@zip.com.au>
Subject: inexact->exact on nan and inf
Date: Wed, 24 Sep 2003 09:50:54 +1000	[thread overview]
Message-ID: <87llsfnjxd.fsf@zip.com.au> (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

                 reply	other threads:[~2003-09-23 23:50 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87llsfnjxd.fsf@zip.com.au \
    --to=user42@zip.com.au \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).