Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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).

share|cite|improve this question
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

You should ask this question in a programming forum.

Iif you're using floating-point numbers, the number you're looking for is stored directly in the bit representation, so you need to ask about library routines to extract the information (or how to get it yourself safely with bit fiddling).

If you're working with integers, what you want is a bit-twiddling method to count the number of leading zeroes in the bit representation, for which standard implementations exist -- and there might even be a built-in calculation available from the compiler you're using, or assembly language instructions you can inline to obtain it.

share|cite|improve this answer
Though the general problem is a programming one, this particular question that we are stuck with is a mathematical, which is why I intentionally posted it here. My problem is not about how to implement it (we're using different technics, like SSE or OpenMP to examine the gain of such technics), but about how to choose m. We're of course using 2 as base so that we can directly manipulate the exponent, by casting the value to int and manipulating the exponent part of the IEEE float value. – pygospa Dec 3 '12 at 2:49
$m$ is the exponent (or maybe one less or one more; I haven't thought it through). – Hurkyl Dec 3 '12 at 3:08

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.