# Algorithm to calculate logarithms in C. Without math.h

posted Sep 16

A simple algorithm explained for the calculation of algorithms of any integer base.

We know that math.h library offers us an extensive number of functions for mathematical calculations. This is the case of the functions to calculate logarithms. The function that is given below is not going to be more than an application of an algorithm. You can achieve good precision, but for practical utilities, it is always recommended to use the functions of the math library.

Algorithm:

Explanation:

This is an example of how we will do it, taking as b = 2 and n = 30. We express it as a power  (step 2) and divide the number n by the b as many times as possible until it is less than b. We have been able to divide it a total of four times (step 3).

We raise to 10 in both limbs and divide by b again as many times as we can ( steps 5 and 6, a 1 is the previous number divided by 2 9 ). If we look we have got the numbers 4 and 9. And we can see that log 2 30 is approximately 4.9.

We will arrive at the perfect result when the second member of the equation is 1, that is, 2 0, being able to have equal bases and thus equal exponents. It would be to clear the x.

Expressed with letters it would be as shown in the image on the left.

We would have that in the first step xi; In the second 10x-10i-j; In the third 10 (10x-10i + j) + k, ...

That is, in each step we will multiply the above by 10, and we will add a new number (given by the number of times that the second member of the equation: n, n 1 ...).

Each time it is multiplied by 100 and that number is added to it. At the end of all, we will equal to 0 (if we get that n becomes 1, the number obtained will be the exact one) that expression. The more steps, the more exact.

For the latter, we will apply a precision number (not to be up to infinity, if that were the case).

Let us express the algorithm:

We have b (base) and n (number) as parameters. We create a variable accurate (precision) and a val = 0.

We will repeat this section until n! = 1 or we have reached the required precision (in each step: reps ++ ).

We divide n by b until n <b. It will give us an i number of times.

val = 10 * ( val -i); (In the first repetition we will have -10i )
n = n ^ 10;
That is, we are in step 4 of the images. The difference is that we are already solving for 10x, 100x, 1000x ... That coefficient will be passed to the other side of the equation at the end.
We obtain the x value by dividing by 10 ^ reps (that is, the number of times we have repeated step 2).
Code in C.

``````double log(int b,double n) {<font></font>
double val = 0;<font></font>
int i,accurate = 10,reps=0;<font></font>
while(n != 1 && accurate>=0) {<font></font>
for(i=0;n>=b;i++) n /= b;<font></font>
n = p(n,10);<font></font>
val = 10*(val+i);<font></font>
accurate--; reps++;<font></font>
}<font></font>
return (double)val/p(10,reps);<font></font>
}``````

We will use a function to raise numbers to integer exponents:

``````double p(double x,int i) {<font></font>
double r = 1.0;<font></font>
for(i;i>0;i--) r *= x;<font></font>
return r;<font></font>
}
``````

31,070 points
9 4 2