* frac -0x80..00 / 0x80..00
@ 2003-11-21 21:00 Kevin Ryde
0 siblings, 0 replies; only message in thread
From: Kevin Ryde @ 2003-11-21 21:00 UTC (permalink / 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
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2003-11-21 21:00 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-11-21 21:00 frac -0x80..00 / 0x80..00 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).