Logarithm Algorithm

前端 未结 1 837
囚心锁ツ
囚心锁ツ 2020-12-24 06:02

I need to evaluate a logarithm of any base, it does not matter, to some precision. Is there an algorithm for this? I program in Java, so I\'m fine with Java code.

Ho

相关标签:
1条回答
  • 2020-12-24 06:19

    Use this identity:

    logb(n) = loge(n) / loge(b)

    Where log can be a logarithm function in any base, n is the number and b is the base. For example, in Java this will find the base-2 logarithm of 256:

    Math.log(256) / Math.log(2)
    => 8.0
    

    Math.log() uses base e, by the way. And there's also Math.log10(), which uses base 10.

    0 讨论(0)
提交回复
热议问题