# Base 2 logarithm with Taylor expansion

I'm trying to implement the natural logarithm in C, and our task is to make it really efficient. So what we are doing is, that we use the first 8 members of the series.

This works fine, but the problem is that our approximation has a large error. Now we were told that we have to find a way to have $|x| \in [0.5, 1]$, so that the error won't get that big. The general idea is to make use of these mathematical equations:

$ln(x) = ln(x\cdot 2^{m - m}) = ln(2^m) + ln(x\cdot 2^{-m}) = m\ln(2) + ln(x\cdot 2^{-m})$

Now the only problem that we have is, that we're somehow stuck in determining a m that fits our purpose.

I hope you'll understand what I'm looking for - to make it more clear here's another example, that may illustrate what I'm looking for:

When implementing the exponential function we used following trick: $e^x = e^{x-k\cdot ln(2) + k\cdot ln(2)} = e^{x-k\cdot ln(2)} \cdot e^{k\cdot ln(2)}$ Now to keep our x in a Range of 0 and ln(2) we defined k as: $k = \lfloor \frac{x}{ln(2)}\rfloor = \lfloor x \cdot ld(e)\rfloor$

In advance, thank you for your help (and sorry for my clumsy language - I'm not used to formulate mathematical problems in english).

-
Can you use frexp? It does exactly the split you want. frexp is easy to implement if you assume IEEE floating-point representation. –  lhf Aug 4 '13 at 1:24

$m$ is the exponent (or maybe one less or one more; I haven't thought it through). –  Hurkyl Dec 3 '12 at 3:08