On Thursday, 26 August 2021 at 22:17:55 UTC+2 kziem...@gmail.com wrote:

> Another topic. I needed to check package "math/bits" (learning about Go 
> can lead us in such places quite fast) and I'm confused about function 
> "Len(x uint) int". In its description we have (
> https://pkg.go.dev/math/bi...@go1.17 <https://pkg.go.dev/math/bits@go1.17>
> )
> BEGINNING
> Len returns the minimum number of bits required to represent x; the result 
> is 0 for x == 0.
> END
> I have no problem with using function that says 0 can be encoded in 0 
> bits, but it is still odd. Maybe it is connected to something done under 
> the hood, about which I don't know a thing? Does anyone know why this 
> choose was made?

 
No, the description doesn't say that 0 can be encoded in 0 bits:
It says that Len(0) returns 0.
If you want Len to be a total function you must return a value for every 
input.
For most inputs the value is strictly determined by what the functions does
(number of bits needed to represent), so Len(9) == 3. But how many bits
do you need to represent 0? The question is malformed as there are no 
set bits in the used representation of 0. One could have declared
    "Len(0) returns -42"
but this makes no sense at all. Having Len(0)==0 results in
Len(a) <= Len(b) if a < b without having to invent totally arbitrary
values for Len(0).

You probably should not overinterpret Go's documentation.
This is not lyric. "the result is 0 for x == 0" has no hidden meaning.

V.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/e6f03535-5fd6-44b4-a866-0382aa13483bn%40googlegroups.com.

Reply via email to