

Where epsilon (error) is the minimum precision. public static double log(double x, double n, double epsilon) You have therefore just implemented logN as well. This is not C, but C#, but I'm sure anybody capable to program in C will be able to deduce the C-Code from that. At the end we will have to add n*ln(2) to get the final result.Īn example implementation for numbers >= 1: float ln(float y) while ((yn - yn1) > epsilon) Divide with that value, then the result gets into the 1.2 interval. That number is actually the base-2 logarithm. So first find the highest power of 2, which is still smaller than the number (See: What is the fastest/most efficient way to find the highest set bit (msb) in an integer in C?). Instead of that, do the following decomposition: Increasing the interval is not wise, because that would increase the maximal error even more. We generated a polynomial which approximates the ln(x), but only inside the interval. I'll show you how to generate one in Maple (it is a computer algebra program), using the Remez algorithm.įor 3 digits of accuracy execute the following commands in Maple: with(numapprox): Most professional implementations are using approximating polynomials. Using Taylor series is not the simplest neither the fastest way of doing this.
