2
$\begingroup$

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

  • 0
    Can you use frexp? It does exactly the split you want. frexp is easy to implement if you assume IEEE floating-point representation.2013-08-04

1 Answers 1

2

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.

  • 0
    $m$ *is* the exponent (or maybe one less or one more; I haven't thought it through).2012-12-03