How do I calculate non-natural logarithms in Racket?
Asked Answered
D

2

5

I am aware of racket's log function, which computers the natural logarithm of a number. I am trying to find the logarithms of numbers raised to arbitrary bases. In other words, instead of this:

> (log 9)
2.1972245773362196

I would like to do something similar to this:

> (logarithm 3 9)
2

Is there a function anyone knows about either builtin to Racket or available in a module from PLaneT I can use like this?

Deepsix answered 7/2, 2013 at 1:15 Comment(0)
H
4

Racket 6.9.0.1 added a second argument for arbitrary bases. logkn can now be written as (log n k).

According to the docs, this is equivalent to (/ (log n) (log k)), but possibly faster.

log entry in the documentation.

Hege answered 19/12, 2017 at 4:17 Comment(0)
C
16

Use math: logk n = ln n / ln k:

(/ (log 9) (log 3))
Charles answered 7/2, 2013 at 1:18 Comment(3)
(expt b (* x y)) is equivalent to (expt (expt b x) y). Since logarithms are the inverse of exponentiation, this explains why the formula works. This was high school algebra when I took it 35 years ago.Varnish
I just taught logarithms in a US college course last term. Currently, some people but not others learn about logarithms in high school.Capstan
It was indeed taught to me in high school--and hence, was completely forgotten about by the time I had to use this fact as a 5th year college student.Bullen
H
4

Racket 6.9.0.1 added a second argument for arbitrary bases. logkn can now be written as (log n k).

According to the docs, this is equivalent to (/ (log n) (log k)), but possibly faster.

log entry in the documentation.

Hege answered 19/12, 2017 at 4:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.