1

I have this bit of VB6 sliced out of a project I'm working on:

Public Function C_Ln(c As ComplexNumber) As ComplexNumber
    Set C_Ln = toComplex(Log(C_Abs(c)), Atan2(c.Imag, c.Real))
End Function

The VB6 Log() function is base-e. I'd like to cook up versions of this to do base-2, base-10 and base-n. Where do I start?

AakashM
  • 62,551
  • 17
  • 151
  • 186
bugmagnet
  • 7,631
  • 8
  • 69
  • 131

2 Answers2

9

You can use the following mathematical identity:

alt text

In VB it would be something like:

Log10 = Log(X) / Log(10)
Log2 = Log(X) / Log(2)
' ... LogN = Log(X) / Log(N)
Community
  • 1
  • 1
molf
  • 73,644
  • 13
  • 135
  • 118
  • and that still works okay for complex numbers? Sorry if I appear naive, it's because I am (w.r.t. complex numbers) – bugmagnet Jun 26 '09 at 09:11
  • 2
    That works fine with complex numbers, however complex logs are infinitely valued. Your formula would calculate only the principal branch, but that's probably good enough. In practice, no one likes an infinitely valued function. – Rhythmic Fistman Jun 26 '09 at 09:21
  • 2
    @boost, yes, see: http://en.wikipedia.org/wiki/Complex_logarithm#Logarithms_to_other_bases (but note that a complex number has more than one logarithm). – molf Jun 26 '09 at 09:26
2

If you divide the natural log of x by the log of the base you want to achieve you get the desired result, i.e. (ln x)/(ln n) = y

See here for an explanation

Rich Seller
  • 83,208
  • 23
  • 172
  • 177