homepage

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.

classification
Title: int(math.log(2**i, 2))
Type: behavior Stage:
Components: Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Alex.Rhatushnyak, mark.dickinson
Priority: normal Keywords:

Created on 2013年05月12日 05:36 by Alex.Rhatushnyak, last changed 2022年04月11日 14:57 by admin. This issue is now closed.

Messages (4)
msg188976 - (view) Author: Alex Rhatushnyak (Alex.Rhatushnyak) Date: 2013年05月12日 05:36
In Python 2.7.4:
import math
for i in range(40, 55): print int(math.log(2**i, 2)),
output:
40 41 42 43 44 45 46 46 48 49 49 50 52 53 53
msg189005 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2013年05月12日 10:33
Sorry: this is not a bug, but a difficult-to-avoid consequence of floating-point imprecision: math.log(n, 2) is computed as log(n) / log(2), and each of the log computations and the division can introduce small errors.
For what it's worth, Python 3.3 has a `log2` function, which has accuracy that's a little bit better than math.log(n, 2), and gives the 'correct' answer exact powers of 2.
Closing as invalid.
msg189006 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2013年05月12日 10:34
Postscript: depending on what you're doing, you might also find the int.bit_length method helpful.
msg189012 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2013年05月12日 10:56
For tracker archaeologists: see also issue #11888, issue #9959.
History
Date User Action Args
2022年04月11日 14:57:45adminsetgithub: 62158
2013年05月12日 10:56:55mark.dickinsonsetmessages: + msg189012
2013年05月12日 10:34:07mark.dickinsonsetmessages: + msg189006
2013年05月12日 10:33:03mark.dickinsonsetstatus: open -> closed

nosy: + mark.dickinson
messages: + msg189005

resolution: not a bug
2013年05月12日 05:36:48Alex.Rhatushnyakcreate

AltStyle によって変換されたページ (->オリジナル) /