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 a bit puzzled.

Oh, it looks like it is defined only in C99, not C90, I think this answers my question. Still seems stupid.

+34
python math
Jun 07 2018-10-06T00:
source share
2 answers
+43
Jun 07 '10 at 21:24
source share

I'm not sure what you want, but:

- From a mathematical point of view, you can do for exmaple math.log (x) /math.log (2).

- If input X is an integer type, and you expect an integral rounded result, you can do it faster, with the right shift. This works with the SHR command and without taylor series + local interpolation, which is under the hood of the libc log () calls.

+3
Mar 12 '14 at 15:51
source share



All Articles