unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
* bug#21883: unnecessary bit shifting range limits
@ 2015-11-12  7:07 Zefram
  2018-10-14  8:18 ` Mark H Weaver
  0 siblings, 1 reply; 5+ messages in thread
From: Zefram @ 2015-11-12  7:07 UTC (permalink / raw)
  To: 21883

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





^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2018-10-15  7:08 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-11-12  7:07 bug#21883: unnecessary bit shifting range limits Zefram
2018-10-14  8:18 ` Mark H Weaver
     [not found]   ` <CAGua6m1D3Ed-T3GJntfZr5QesTvR_Gf1-JbsiAetdoxBGkce_w@mail.gmail.com>
     [not found]     ` <CAGua6m1CrfW2jEmQcDn_-1szGQbNcboBa45M6inEE5As6FToXw@mail.gmail.com>
2018-10-14  9:46       ` Stefan Israelsson Tampe
2018-10-14 22:19         ` Mark H Weaver
2018-10-15  7:08           ` Stefan Israelsson Tampe

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).