Nasser
Nasser

Reputation: 2140

Calculate logarithm in python

I am wondering why the result of log base 10 (1.5) in python is 0.405465108108 while the real answer is 0.176091259.

This is the code that I wrote:

import math

print math.log(1.5)

Can someone tell how to solve this issue?

Upvotes: 42

Views: 128725

Answers (4)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798436

From the documentation:

With one argument, return the natural logarithm of x (to base e).

With two arguments, return the logarithm of x to the given base, calculated as log(x)/log(base).

But the log10 is made available as math.log10(), which does not resort to log division if possible.

Upvotes: 53

JDurstberger
JDurstberger

Reputation: 4255

If you use log without base it uses e.

From the comment

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

Therefor you have to use:

import math
print( math.log(1.5, 10))

Upvotes: 21

user1129665
user1129665

Reputation:

The math.log function is to the base e, i.e. natural logarithm. If you want to the base 10 use math.log10.

Upvotes: 11

utkbansal
utkbansal

Reputation: 2817

math.log10(1.5)

Use the log10 function in the math module.

Upvotes: 14

Related Questions