1

I'm working on a program where the problem has a natural logarithmic scale.

So far I'm using base 2, with a nice implementation of unsigned int log2(uint64_t) (in C/C++), found here.

However, I found that base 2 is too much for my problem: I need to use fractional bases, e.g. 3/2.

Is anyone aware of an implementation for this kind of operation?

My current solution is round(log(x)/log(base)) where round returns integer, but I was hopping, at least, to avoid two evaluations of the log.

Community
  • 1
  • 1
Jorge Leitao
  • 19,085
  • 19
  • 85
  • 121

1 Answers1

2

log(base) is a constant, so just evaluate it once and take its reciprocal (to turn it onto a multiply rather than an expensive divide).

const float k = 1.0f / log(base); // init constant once

y = round(log(x) * k); // each evaluation only requires one log,
                       // one multiply and one round
Paul R
  • 208,748
  • 37
  • 389
  • 560