2

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?

2
  • 1
    Sidenote: you can from math import log and save yourself a line. Commented Feb 2, 2016 at 21:04
  • 1
    It always helps to read the docs: docs.python.org/2/library/math.html. This is important especially when using a logarithm function since log() can mean either base 10 or base e depending on the programmer. Commented Feb 2, 2016 at 22:14

2 Answers 2

11

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).

Sign up to request clarification or add additional context in comments.

3 Comments

Alternatively, you can use math.log10()
@Code-Apprentice That is true. I have updated my answer.
Thank you for this answer, it certainly solved my issue and now its been layed out makes me feel a bit stupid as i was actually close to this answer from reading the documentation but simply didn't implement it correctly!! however I now simply start my code with import math log = math.log10 instead of doing it in the equation as x, log(x) is derived from a simple division using variables causing additional complication and required me to learn/use the float function. so now looks somthing like this >>> rxcalc_dst = pntsrc_spl + 20 * log(float(pntsrc_dst) / float(pntrx_dst)) Thanks again!
1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.