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?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With