unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Kevin Ryde <user42@zip.com.au>
Subject: frac -0x80..00 / 0x80..00
Date: Sat, 22 Nov 2003 07:00:29 +1000	[thread overview]
Message-ID: <87llq9phci.fsf@zip.com.au> (raw)

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

I think scm_make_ratio is missing one case of an integer result,
namely when the numerator is most-negative-fixnum and the denominator
is the negative of that (as a bignum).  Viz,

	(integer? (/ -536870912 536870912))
	=> #f

where I would expect #t.  (Attempting to actually print that division
seems to go into an infinite recursion in number->string.)

        * numbers.c (scm_make_ratio): Check for numerator equal to
        SCM_MOST_NEGATIVE_FIXNUM and bignum denominator the negative of that,
        giving integer -1.

        * tests/fractions.test: Exercise most-negative-fixnum over -ve of
        most-negative-fixnum.


[-- Attachment #2: numbers.c.make-frac.diff --]
[-- Type: text/plain, Size: 892 bytes --]

--- numbers.c.~1.215.~	1970-01-01 10:00:01.000000000 +1000
+++ numbers.c	2003-11-21 17:36:45.000000000 +1000
@@ -357,18 +357,26 @@
   */
   if (SCM_INUMP (numerator))
     {
+      long  x = SCM_INUM (numerator);
       if (SCM_EQ_P (numerator, SCM_INUM0))
 	return SCM_INUM0;
       if (SCM_INUMP (denominator))
 	{
-	  long x, y;
-	  x = SCM_INUM (numerator);
+	  long y;
 	  y = SCM_INUM (denominator);
 	  if (x == y)
 	    return SCM_MAKINUM(1);
 	  if ((x % y) == 0)
 	    return SCM_MAKINUM (x / y);
 	}
+      else
+        {
+          /* When x == SCM_MOST_NEGATIVE_FIXNUM we could have the negative
+             of that value for the denominator (as a bignum of course).  */
+          long  abs_x = (x >= 0 ? x : -x);
+          if (mpz_cmpabs_ui (SCM_I_BIG_MPZ (denominator), abs_x) == 0)
+	    return SCM_MAKINUM(-1);
+        }
     }
   else if (SCM_BIGP (numerator))
     {

[-- Attachment #3: fractions.test.most-neg.diff --]
[-- Type: text/plain, Size: 458 bytes --]

--- fractions.test.~1.2.~	1970-01-01 10:00:01.000000000 +1000
+++ fractions.test	2003-11-21 17:27:24.000000000 +1000
@@ -25,6 +25,10 @@
   (testeqv 3/4 3000000000000/4000000000000)
   (testeqv 3 3/1)
   (test= 1/3 (/ 1.0 3.0))
+
+  (test= -1 (/ most-negative-fixnum (- most-negative-fixnum)))
+  (testeq #t (integer? (/ most-negative-fixnum (- most-negative-fixnum))))
+
   (testeqv (+ 1/4 1/2) 3/4)
   (testeqv (* 1/4 2/3) 1/6)
   (testeqv (/ 1/4 2/3) 3/8)

[-- 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-11-21 21:00 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=87llq9phci.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).