Bit length of int or long?

Tim Peters tim_one at
Fri May 19 10:10:13 CEST 2000

[Michael Hudson]
> ...
> Because:
> >>> def bitlen(x):
> ...  assert x > 0
> ...  return math.floor (math.log10(x) / math.log10(2) ) + 1
> ...
> >>> bitlen(2**10000L)
> inf
> (which surprised me; I'm sure that used to raise an exception - has
> this changed recently?)

What the math functions do is mostly inherited directly from the platform
libc, but confused by some #ifdef'ed code in mathmodule.c.  More likely
you're using a different libc, or platform, than you used before (that is, I
don't see anything relevant in Python that's changed recently).  As time
goes on, vendors move more and more to 754 semantics (part of which is
silently returning infinity for overflows).  To the extent that Python pays
any attention at all now, it sporadically makes attempts to follow an old
errno-based EDOM/ERANGE/etc model.  And given the many inconsistencies in fp
behavior across platform C implementations, Python really could't do much
better now without an enormous amount of delicate platform-specific
wormarounds.  Once C9X is widely implemented (see "C9X" posting of
yesterday), it should be possibile to do much better with reasonable effort.

    unpredictability-ly yh'rs  - tim

More information about the Python-list mailing list