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 mark.dickinson
Recipients akira, mark.dickinson
Date 2010-09-27.14:09:21
SpamBayes Score 3.1356844e-09
Marked as misclassified No
Message-id <1285596564.38.0.245989378177.issue9959@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2010-09-27 14:09:24mark.dickinsonsetrecipients: + mark.dickinson, akira
2010-09-27 14:09:24mark.dickinsonsetmessageid: <1285596564.38.0.245989378177.issue9959@psf.upfronthosting.co.za>
2010-09-27 14:09:22mark.dickinsonlinkissue9959 messages
2010-09-27 14:09:21mark.dickinsoncreate