Floating point base 2 logarithm

Started by
16 comments, last by Ectara 11 years, 11 months ago
Sorry, I really wish I could be of more help, but here's a link to a paper:
http://www.icsi.berk...s/TR-07-002.pdf

... which gives a link to the site:
http://www.flipcode...._Function.shtml

... which gives the code:

inline float fast_log2(float val)
{
int *const exp_ptr = reinterpret_cast<int *>(&val); // For interpreting the float as a sequence of bits.
int x = *exp_ptr; // This is obviously not the same thing as truncating the float by casting it as an int.
const int log_2 = ((x >> 23) & 255) - 128;

x &= ~(255 << 23);
x += 127 << 23;

*exp_ptr = x; // This is obviously not the same thing as casting the int as a float.

val = ((-1.0f/3.0f) * val + 2) * val - 2.0f/3.0f;

return val + log_2;
}


... in which -- at the end of the function -- the variable val is within the range of 1 to 2, and the variable log_2 contains the rest of the final result. The precision is not perfect, but it's a start. Best of luck.
Advertisement
A quick search turned up http://www.netlib.org/cephes/ which seems to have C source code for all sorts of operations, including base 2 log.
I did see that; I have an inaccurate approximation, but for this purpose, I need accuracy over speed; as long as it works to comparable precision with the standard C library's log functions.

A quick search turned up http://www.netlib.org/cephes/ which seems to have C source code for all sorts of operations, including base 2 log.


Sweet! Thank you for this.

It's a "language with C syntax", I guess. No assembly, any processor with IEEE 754 support and at least 32bit two's complement integers. I can't use the standard library, but I have a great deal of replacements for most of the modules. Except the math.

For future reference, details like this are kind of a big deal.

You haven't been exactly clear on what you can/can't do with this "language" you're using, but why not use the algorithm on Wikipedia's Binary logarithm page? And if you can't do that, you're going to have to be very, very clear about what your limitations are.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
I may be able to rewrite the Python example. This might be what I'm looking for, thank you. I'll be sure to post again if I still have trouble.

And as for my platform, I have access to all of C89 syntax, with painstakingly written replacements for almost all of the modules, most of which are beyond the scope of this question. With the exception of logarithms and such because, well, I'm writing them now.
Yep, that's what you want to do, as per the algorithm from Wikipedia...
Divide the value by the integer log2 of it leaving you with the decmial portion.
Double the value. It will always be in the range [1,2) at this point.
Then reapeat until the desired precision is reached:

  • Square the value.
  • If it is greater than 2, halve it and output a one into the next significant bit of the result
    else output a zero into the next significant bit of the result.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
I got it working, thank you all for your help.

This topic is closed to new replies.

Advertisement