[issue9959] int(math.log(4,2)) == 1 instead of 2

Mark Dickinson report at bugs.python.org
Mon Sep 27 16:09:24 CEST 2010


Mark Dickinson <dickinsm at gmail.com> added the comment:

No, it's not really a bug:  math.log(x, 2) isn't an atomic operation: it's computed internally as something like log(x) / log(2), and since each of the three steps (computation of the logs, division) can introduce a small rounding error, you shouldn't be surprised if the result doesn't come out exactly.

The difference between 3.1 and 3.2 was a side-effect of a change to remove the dependence of the math module on the internal long integer representation.

Having said that, it would be possible to improve the algorithm that's used to compute log of an integer (see the loghelper function in Modules/mathmodule.c): currently, for positive k, log(2**k) ends up being computed as log(0.5) + (k+1)*log(2), which doesn't help;  it would be slightly better if it were computed directly as k*log(2) instead.  But you still can't (and shouldn't) rely on log(x, 2) giving exact results for powers of 2.

I'm not sure what you're using math.log(x, 2) for, but you may be interested in the int.bit_length method.

----------
assignee:  -> mark.dickinson
nosy: +mark.dickinson

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue9959>
_______________________________________


More information about the Python-bugs-list mailing list