21

Is there a function or any other way to calculate in C the logarithm of base x, where x is an integer variable of my program?

nikos
  • 2,893
  • 11
  • 30
  • 39

1 Answers1

41

C doesn't provide functions to compute logarithms of any bases other than e or 10.

So just use math:

logarithm of x base b = log(x)/log(b)

If you'll be doing the logarithms over the same base repeatedly, you can precompute 1/log(b).
I wouldn't rely on the compiler being able to do this optimization for you.

Mysticial
  • 464,885
  • 45
  • 335
  • 332