Log base 2 of large integers

Peter Otten __peter__ at web.de
Wed Aug 13 09:58:02 EDT 2014


Mok-Kong Shen wrote:
 
> I like to compute log base 2 of a fairly large integer n but
> with math.log(n,2) I got:
> 
> OverflowError: long int too large to convert to float.
> 
> Is there any feasible work-around for that?

What version of Python are you using? Python 2.7 can handle "fairly large" 
integers:

>>> float(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: long int too large to convert to float
>>> math.log(x, 2)
50462500.07504181

Or maybe our idea of "fairly large" differ; so how large is fairly large?




More information about the Python-list mailing list