-1

I'm just trying out maths functions in Python as I'm very new to it and I've noticed something when using e. I'm trying to work out log(1+e^2) which I know from using a calculator is 0.9237 but when I type this out in PyCharm, I get 2.1269.

I've tried setting x = (1+e^2) then doing log(x) in case it's working things out in the wrong order. But even if you get 1+e, then square it and log that number, you get 1.14.

Anyone able to point out what's going wrong here?

Erik
  • 503
  • 1
  • 7
  • 26
  • But what logarithm base do you need? log(1+e^2)=2.1269... is right because "log" by default uses base e. Which is mentioned in its documentation. – h4z3 Jan 16 '20 at 15:11
  • https://docs.python.org/3/library/math.html#math.log "With one argument, return the natural logarithm of x (to base e)." – h4z3 Jan 16 '20 at 15:12
  • No I used (1+math.exp(2)) for (1+e^2). I did not realise it used base e and not base 10, thank you! – Successful-Standard Jan 16 '20 at 15:13
  • (Never mind, `e^2` is a type error, but please ensure your question reflects *exactly* what you are trying to do.) – chepner Jan 16 '20 at 15:13
  • 2
    Your question is lacking critical parts which leads to confusion. Check [\[SO\]: How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) or [\[SO\]: How to create a Minimal, Reproducible Example (reprex (mcve))](https://stackoverflow.com/help/mcve) for more asking related details. Add the *Python* code, and the sequence of keys you press on the calculator. It's obviously the base (or *lg* vs *ln*) in your case, but still – CristiFati Jan 16 '20 at 15:15

2 Answers2

0

Your calculator is using log to the base 10, while python's math library's log is log to the base e.

If you need log to the base 10 in python, use the log10 function in the math library instead.

GoodDeeds
  • 7,956
  • 5
  • 34
  • 61
0

math.log in Python is the natural logarithm (base e), sometimes called ln, especially on calculators. log on your calculator is the base 10 logarithm, which is math.log10 in Python.

Alex Hall
  • 34,833
  • 5
  • 57
  • 89