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?

Alex V
  • 3,416
  • 2
  • 33
  • 52

2 Answers2

16

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

(/ (log 9) (log 3))
nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • 5
    `(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. – Barmar Feb 07 '13 at 01:58
  • I just taught logarithms in a US college course last term. Currently, some people but not others learn about logarithms in high school. – Omar Antolín-Camarena Feb 07 '13 at 20:56
  • 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. – ApproachingDarknessFish Oct 14 '18 at 04:27
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.

Kisaragi Hiu
  • 202
  • 2
  • 8