Davros
Davros

Reputation: 39

math.log(x) returns unexpected results in Python

I am attempting to do some basic dB calculations using Python. If I use either Excel or a scientific calculator:

20*log(0.1) = -20

in Python:

20*log(0.1) = -46.0517018599

to simplify further, Excel and Scientific calc:

log(0.1) = -1

Python:

log(0.1) = -2.30258509299

I start my script with

import math

log = math.log

Can someone explain why this is and how I can fix it?

Upvotes: 2

Views: 1952

Answers (2)

zondo
zondo

Reputation: 20336

math.log is actually ln (with a base of e). To get the expected results, use math.log(0.1, 10) or math.log10(0.1).

Upvotes: 11

bibi
bibi

Reputation: 3765

If you want the base 10 logarithm, you have to use math.log10(0.1)

Documentation is HERE

Fortran, C, C++, Java, tcl and many others they all use log as base-n logarithm (natural logarithm)

Upvotes: 1

Related Questions