Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (20.3k points)

How should I compute log to the base two in python. Eg. I have this equation where I am using log base 2

import math

e = -(t/T)* math.log((t/T)[, 2])

2 Answers

0 votes
by (40.7k points)

Package math.log takes an optional second argument which allows you to specify the base:

In [22]: import math

In [23]: math.log?

Type:       builtin_function_or_method

Base Class: <type 'builtin_function_or_method'>

String Form:    <built-in function log>

Namespace:  Interactive

Docstring:

    log(x[, base]) -> the logarithm of x to the given base.

    If the base not specified, returns the natural logarithm (base e) of x.

In [25]: math.log(8,2)

Out[25]: 3.0

0 votes
by (106k points)

You can use the below-mentioned code:-

import math 

log2 = math.log(x, 2.0) 

log2 = math.log2(x)

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Nov 19, 2020 in Python by ashely (50.2k points)
0 votes
2 answers

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...