Author fredrikj
Recipients fredrikj, loewis, rhettinger
Date 2008-07-24.23:12:13
SpamBayes Score 6.62469e-08
Marked as misclassified No
Message-id <>
Raymond, yes, I think that a separate numbits function would better,
although exposing this functionality would not prevent also changing the
behavior of frexp. As I said, math.log already knows about long
integers, so handling long integers similarly in frexp would not be all
that unnatural. (On the other hand, it is true that math.sqrt, math.pow,
math.cos, etc could all theoretically be "fixed" to work with
larger-than-double input, and that slippery slope is probably better

Good point about roundtripping, but the problem with that argument is
that frexp already accepts integers that cannot be represented exactly,

>>> ldexp(*frexp(10**100)) == 10**100

Anyway, if there is support for exposing _PyLong_Numbits, should it be a
method or a function? (And if a function, placed where? Should it accept
floating-point input?)

I'm attaching a patch (for the trunk) that adds a numbits method to the
int and long types. My C-fu is limited, and I've never hacked on Python
before, so the code is probably broken or otherwise bad in several ways
(but in that case you can tell me about it and I will learn something
:-). I did not bother to optimize the implementation for int, and the
tests may be redundant/incomplete/placed wrongly.

A slight problem is that _PyLong_NumBits is restricted to a size_t, so
it raises an OverflowError on 32-bit platforms for some easily
physically representable numbers:

>>> (1<<3*10**9).numbits()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: long int too large to convert to int

This would need to be fixed somehow.

If numbits becomes a method, should it also be added to the Integral
ABC? GMPY's mpz type, by the way, defines a method numdigits(self,
base). This generalization would possibly be overkill, but it's worth

If it's too late to add this method/function for 2.6 and 3.0, please
update the issue version field as appropriate.
Date User Action Args
2008-07-24 23:12:23fredrikjsetspambayes_score: 6.62469e-08 -> 6.62469e-08
recipients: + fredrikj, loewis, rhettinger
2008-07-24 23:12:22fredrikjsetspambayes_score: 6.62469e-08 -> 6.62469e-08
messageid: <>
2008-07-24 23:12:20fredrikjlinkissue3439 messages
2008-07-24 23:12:18fredrikjcreate