Thursday, April 21, 2011

log2 in python math module

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), but log2 is really common, so I'm kind of baffled.

Oh, it looks like it's only defined in C99, not C90, I guess that answers my question. Still seems kind of silly.

From stackoverflow
  • 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.

    Mark Ransom : That begs the question, why does `log10` exist?
    Mark Dickinson : log10 is C89, so it exists on all common platforms, including Windows. So it's trivial for Python to add a wrapper round it.
    nmaxwell : Well, fair enough. I guess people like log10 enough to include it specially but not log2. It looks like it's actually in numpy, so that solves that.

0 comments:

Post a Comment