Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logarithm function of an arbitrary integer base in C

Tags:

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?

like image 564
nikos Avatar asked Jun 15 '12 16:06

nikos


People also ask

What is the log function in C?

C log() Prototype The log() function takes a single argument and returns a value of type float . [Mathematics] logex = log(x) [In C programming] It is defined in <math. h> header file. In order to find the log() of long double or float numbers, you can use the following prototype.

Is log () Same as log10 () r?

Apart from log() function, R also has log10() and log2() functions. basically, log() computes natural logarithms (ln), log10() computes common (i.e., base 10) logarithms, and log2() computes binary (i.e., base 2) logarithms. The general form logb(x, base) computes logarithms with base mentioned.


1 Answers

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.

like image 72
Mysticial Avatar answered Oct 09 '22 07:10

Mysticial