* integer-length on negative bignum
@ 2003-05-03 23:55 Kevin Ryde
0 siblings, 0 replies; only message in thread
From: Kevin Ryde @ 2003-05-03 23:55 UTC (permalink / raw)
[-- Attachment #1: Type: text/plain, Size: 584 bytes --]
A small bug in integer-length, assuming I understand what that
function is meant to be doing. This lets it correspond to the inum
case at least.
* numbers.c (scm_integer_length): On negative bignums, adjust
mpz_sizeinbase to account for it looking at absolute value where we
want ones-complement.
* tests/numbers.test (integer-length): Exercise some negatives, in
particular "...11100..00".
In the tests here, it's only ...11100..00 which is affected by the
problem, the others are already right, I just added them for coverage
and confirmation of what integer-length does.
[-- Attachment #2: numbers.c.integer-length.diff --]
[-- Type: text/plain, Size: 756 bytes --]
--- numbers.c.~1.181.~ 2003-05-04 09:09:49.000000000 +1000
+++ numbers.c 2003-05-04 09:44:31.000000000 +1000
@@ -1438,7 +1438,14 @@
};
return SCM_MAKINUM (c - 4 + l);
} else if (SCM_BIGP (n)) {
+ /* mpz_sizeinbase looks at the absolute value of negatives, whereas we
+ want a ones-complement. If n is ...111100..00 then mpz_sizeinbase is
+ 1 too big, so check for that and adjust. */
size_t size = mpz_sizeinbase (SCM_I_BIG_MPZ (n), 2);
+ if (mpz_sgn (SCM_I_BIG_MPZ (n)) < 0
+ && mpz_scan0 (SCM_I_BIG_MPZ (n), /* no 0 bits above the lowest 1 */
+ mpz_scan1 (SCM_I_BIG_MPZ (n), 0)) == ULONG_MAX)
+ size--;
scm_remember_upto_here_1 (n);
return SCM_MAKINUM (size);
} else {
[-- Attachment #3: numbers.test.integer-length.diff --]
[-- Type: text/plain, Size: 748 bytes --]
--- numbers.test.~1.17.~ 2003-05-04 09:11:34.000000000 +1000
+++ numbers.test 2003-05-04 09:41:02.000000000 +1000
@@ -1739,3 +1739,32 @@
;;;
;;; inexact->exact
;;;
+
+;;;
+;;; integer-length
+;;;
+
+(with-test-prefix "integer-length"
+
+ (with-test-prefix "negatives ...11100..00"
+ (do ((n -1 (ash n 1))
+ (i 0 (1+ i)))
+ ((> i 256))
+ (pass-if (list n "expect" i)
+ (= i (integer-length n)))))
+
+ (with-test-prefix "negatives ...11100..01"
+ (do ((n -3 (logxor 3 (ash n 1)))
+ (i 2 (1+ i)))
+ ((> i 256))
+ (pass-if n
+ (= i (integer-length n)))))
+
+ (with-test-prefix "negatives ...111011..11"
+ (do ((n -2 (1+ (ash n 1)))
+ (i 1 (1+ i)))
+ ((> i 256))
+ (pass-if n
+ (= i (integer-length n))))))
+
+
[-- 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-05-03 23:55 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-05-03 23:55 integer-length on negative bignum 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).