[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
bug#40855: integer-length 0 should be 1
From: |
tomas |
Subject: |
bug#40855: integer-length 0 should be 1 |
Date: |
Sun, 26 Apr 2020 10:23:15 +0200 |
User-agent: |
Mutt/1.5.21 (2010-09-15) |
On Sat, Apr 25, 2020 at 11:15:40PM +0200, Bengt Richter wrote:
> I'm hoping this is the right place to report this bug :)
>
> To reproduce, guile -c '(format #t "~s\n" (integer-length 0))'
> Expected result if correct: 1
> Observed result: 0
>
> The following is to support the opinion that 1 is the correct result,
> and to explore how integer "length" generalizes to other radixes and
> also signed number writing other than sign-magnitude.
Hm. This is a tough one. The problem is that there are several
possible extensions to zero, and no one is quite right. For example,
one could interpret (integer-length n) as one plus the base-two
logarithm of n (well, its integer part). Or, in mathy jargon,
1 + floor(log2 n).
In this case, the integer-length of zero would be minus infinity!
And for negative n, we'd be in hot water (or in complex analysis or
something :)
Thus, the simple definition given in the doc "For positive N this
is how many bits to the most significant one bit" looks like a wise
choice to me -- and this leads to a value of 0 for 0 (or some
infinity, if you keep searching for the one bit you'll never find,
if you insist to find an one.
Cheers
-- tomás
signature.asc
Description: Digital signature