log2 in python math module

后端 未结 2 1530
甜味超标
甜味超标 2021-02-05 00:22

why doesn\'t it exist?

import math
[x for x in dir(math) if \'log\' in x]
>>> [\'log\', \'log10\', \'log1p\']

I know I can do log(x,2)

2条回答
  •  隐瞒了意图╮
    2021-02-05 00:38

    I think you've answered your own question. :-) There's no log2(x) because you can do log(x, 2). As The Zen of Python (PEP 20) says, "There should be one-- and preferably only one --obvious way to do it."

    That said, log2 was considered in Issue3366 (scroll down to the last 3 messages) which added several other C99 math functions to the math module for Python 2.7 and 3.2.

    Edit: log2 was reconsidered in Issue11888 and added in Python 3.3.

提交回复
热议问题