From: Kevin Ryde <user42@zip.com.au>
Subject: integer-length on negative bignum
Date: Sun, 04 May 2003 09:55:52 +1000 [thread overview]
Message-ID: <87d6izlh4n.fsf@zip.com.au> (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
reply other threads:[~2003-05-03 23:55 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=87d6izlh4n.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).