GNU bug report logs - #21883
unnecessary bit shifting range limits

Previous Next

Package: guile;

Reported by: Zefram <zefram <at> fysh.org>

Date: Thu, 12 Nov 2015 07:08:01 UTC

Severity: normal

Full log


View this message in rfc822 format

From: Zefram <zefram <at> fysh.org>
To: 21883 <at> debbugs.gnu.org
Subject: bug#21883: unnecessary bit shifting range limits
Date: Thu, 12 Nov 2015 07:07:25 +0000
Not really outright bugs, but these responses are less than awesome:

$ guile -c '(write (logbit? (ash 1 100) 123))'
ERROR: Value out of range 0 to 18446744073709551615: 1267650600228229401496703205376
$ guile -c '(write (ash 0 (ash 1 100)))'
ERROR: Value out of range -9223372036854775808 to 9223372036854775807: 1267650600228229401496703205376
$ guile -c '(write (ash 123 (ash -1 100)))'
ERROR: Value out of range -9223372036854775808 to 9223372036854775807: -1267650600228229401496703205376

In all three cases, the theoretically-correct result of the expression
is not only representable but easily computed.  The functions could be
improved to avoid failing in these cases, by adding logic amounting to:

(define (better-logbit? b v)
  (if (>= b (integer-length v)) (< v 0) (logbit? b v)))

(define (better-ash v s)
  (cond
    ((= v 0) 0)
    ((<= s (- (integer-length v))) (if (< v 0) -1 0))
    (else (ash v s))))

-zefram




This bug report was last modified 6 years and 305 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.