3

I'm having problems using Math.log in C#, it's giving me wrong values.

In my code:

Math.log(0.137931034482758)  

returns: -1.98100146886658

but with a calculator

log(0.137931034482758)  

gives -0.86033800657099

Why is that?

Miç Miç
  • 49
  • 3
  • 4
    You should learn mathematics before. `Math.log` returns the neperian logarithm (https://msdn.microsoft.com/en-us/library/x80ywz41(v=vs.110).aspx) while `log` on your calculator returns the decimal logarithm, they're the same except for a factor. – Jean Rostan Apr 25 '18 at 14:04
  • 4
    I suspect you're using log base 10 on the calculator, whereas `Math.Log` returns log base e. – Jon Skeet Apr 25 '18 at 14:04

2 Answers2

7

There seems to a bit of confusion between log(n) and ln(n) where log(n) is really log10(n) and ln(n) is the natural logarithm and is equivalent to loge(n) and

So, mathematically

log(0.137931034482758) = -0.86033800657099565123053753815789

but

ln(0.137931034482758) = -1.9810014688665879083488077894557

Looks like Math.Log uses the natural logarithm.

If you want log10(n) then use Math.Log10(n) or Math.Log(n, 10) instead.

phuzi
  • 12,078
  • 3
  • 26
  • 50
  • 2
    Keep in mind that `Math.Log10(1000) == Math.Log(1000, 10)` is [`False`](https://repl.it/@baliibrahim/LogDifference) – Nae Oct 05 '20 at 20:06
  • 1
    Also see [this](https://stackoverflow.com/q/1497400/7032856) perhaps. – Nae Oct 05 '20 at 20:47
0

As Daisy sais:

log(-1.98100146886658) with basis e is -1.98100146886658

log(-1.98100146886658) with basis 10 is -0.86033800657099

Math.log uses per default e as a basis https://msdn.microsoft.com/de-de/library/x80ywz41(v=vs.110).aspx

And your calculator obviously uses uses basis 10.

misanthrop
  • 771
  • 7
  • 32