This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author fredrikj
Recipients fredrikj
Date 2008-07-24.17:20:42
SpamBayes Score 2.5003362e-07
Marked as misclassified No
Message-id <>
Python 3.0b2 (r30b2:65106, Jul 18 2008, 18:44:17) [MSC v.1500 32 bit
(Intel)] on
Type "help", "copyright", "credits" or "license" for more information.
>>> import math
>>> math.frexp(10**100)
(0.5714936956411375, 333)
>>> math.frexp(10**1000)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: Python int too large to convert to C double

(Same behavior in Python 2.5.2, and presumably 2.6 although I haven't
checked the latter.)

I think it should be easy to make frexp work for large integers by
calling PyLong_AsScaledDouble and adding the exponents. It would be
logical to fix this since math.log(n) already works for large integers.

My reason for requesting this change is that math.frexp is the fastest
way I know of to accurately count the number of bits in a Python integer
(it is more robust than math.log(n,2) and makes it easy to verify that
the computed size is exact) and this is something I need to do a lot.

Actually, it would be even more useful to have a standard function to
directly obtain the bit size of an integer. If I'm not mistaken,
PyLong_NumBits does precisely this, and would just have to be wrapped.
Aside from my own needs (which don't reflect those of the Python
community), there is at least one place in the standard library where
this would be useful: contains an inefficient implementation
(_nbits) that could removed.
Date User Action Args
2008-07-24 17:20:45fredrikjsetspambayes_score: 2.50034e-07 -> 2.5003362e-07
recipients: + fredrikj
2008-07-24 17:20:44fredrikjsetspambayes_score: 2.50034e-07 -> 2.50034e-07
messageid: <>
2008-07-24 17:20:43fredrikjlinkissue3439 messages
2008-07-24 17:20:42fredrikjcreate