Search code examples
mathschemeracketlogarithm

How to define logarithms for base other than e?


I'm trying to define log in Scheme. For example if

(log (exp 1)) ;=> 1.0

Then, how could I write log5(25)?


Solution

  • The log function in scheme yields the natural logarithm (base e) of a number. In order to compute the logarithm of a different base, you simply divide the loge of a number by the loge of the desired base.

    To define a log5 function in Scheme:

    (define (log5 x) (/ (log x) (log 5)))
    

    Then (log5 25) will yield 2.0