Started by May 11 2012 02:30 PM

,
17 replies to this topic

Posted 11 May 2012 - 02:30 PM

How would one interpolate between logarithmic values? For a bit of context, say I have a floating point value. I can get the integer part of the logarithm by taking the exponent, or in the case of an unnormalized number, the base 2 logarithm of the mantissa and adjust it to where it should lie on the spectrum. However, I would also like to get the fractional part of the logarithm; somehow, using the mantissa, I must get the fractional part of the resulting logarithm that is somewhere between the floored and the ceilinged logarithm. Does anyone know how to do this? Clearly, linear interpolation won't cut it.

Posted 11 May 2012 - 02:37 PM

What are the integer and fractional parts of a logarithm? Log is perfectly well defined for floating point numbers (well positive ones), integral or not should not figure into it.

Posted 11 May 2012 - 02:51 PM

Try this:

#include <math.h> double log2(double x) { return log(x) / log(2.0); }

Posted 11 May 2012 - 03:51 PM

Like the above two people, I am having some difficulty understanding what you're asking for.

Anyway, maybe look into frexp()? Don't be deceived by the word "exp" in this particular function's name -- it's base 2, not base e. I mean, is 8.1 == 0.506250 * pow(2.0, 4.0) along the lines of what you mean by splitting between fractional and integer parts, where 0.506250 is the fractional part and 4 is the integer part? If so, then frexp() is the function that you're looking for.

Anyway, maybe look into frexp()? Don't be deceived by the word "exp" in this particular function's name -- it's base 2, not base e. I mean, is 8.1 == 0.506250 * pow(2.0, 4.0) along the lines of what you mean by splitting between fractional and integer parts, where 0.506250 is the fractional part and 4 is the integer part? If so, then frexp() is the function that you're looking for.

**Edited by taby, 11 May 2012 - 04:06 PM.**

Posted 11 May 2012 - 04:06 PM

No, these are not what I'm looking for; while alvaro is mathematically correct, I have no logarithm function to work with. Basically, I can easily return the integer part of y, where y = log2(x). However, I am having trouble finding the fractional part. For instance:

log2(12394) = 13.5973543.

I can easily get the answer 13, however I have remaining significant bits that I would like to use to determine the remaining 0.5973543.Has anyone done this sort of thing before?

log2(12394) = 13.5973543.

I can easily get the answer 13, however I have remaining significant bits that I would like to use to determine the remaining 0.5973543.Has anyone done this sort of thing before?

**Edited by Ectara, 11 May 2012 - 04:07 PM.**

Posted 11 May 2012 - 04:13 PM

No, these are not what I'm looking for; while alvaro is mathematically correct, I have no logarithm function to work with. Basically, I can easily return the integer part of y, where y = log2(x). However, I am having trouble finding the fractional part. For instance:

log2(12394) = 13.5973543.

I can easily get the answer 13, however I have remaining significant bits that I would like to use to determine the remaining 0.5973543.Has anyone done this sort of thing before?

modf() will give you both the numbers 13 and 0.5973543.

fmod() will give you 0.5973543 if you already have 13 on hand and want to leverage it as input (though this route seems a bit wasteful).

**Edited by taby, 11 May 2012 - 04:20 PM.**

Posted 11 May 2012 - 04:18 PM

No, that's not what I'm looking for. I get an incomplete answer with my current implementation of log2(). I need to get more information, not less.

Posted 11 May 2012 - 04:24 PM

No, that's not what I'm looking for. I get an incomplete answer with my current implementation of log2(). I need to get more information, not less.

Oh, I see what you mean now. You're literally manipulating this on the level of bits because you're being forced to write your own floating point type, and/or you're stuck at the part where you have to write your own base 2 logarithm function? If you could show us what you already have, that'd be super great, otherwise I'm just going to suggest that you stick with the standard library -- which clearly already does everything that you need.

**Edited by taby, 11 May 2012 - 04:43 PM.**

Posted 11 May 2012 - 04:38 PM

No, these are not what I'm looking for; while alvaro is mathematically correct, I have no logarithm function to work with. Basically, I can easily return the integer part of y, where y = log2(x). However, I am having trouble finding the fractional part. For instance:

log2(12394) = 13.5973543.

I can easily get the answer 13, however I have remaining significant bits that I would like to use to determine the remaining 0.5973543.Has anyone done this sort of thing before?

The thread is marked "C language", and the C language that come with a library that includes `log'. If your circumstances are different, you are going to have to explain them carefully. Do you have access to assembly? For what processor? What other restrictions do you have?

EDIT: Although I still would like to know the answers to the questions above, I am pretty sure you are looking for something like this: http://www.quinapalus.com/efunc.html

**Edited by alvaro, 11 May 2012 - 04:42 PM.**

Posted 11 May 2012 - 05:07 PM

This is what I have so far, to return the floored base 2 logarithm. It's a little obfuscated, but mostly working. However, I have a version returning a floating point type that isn't too different, seeing as the fractional part of the answer isn't being returned properly.

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.

That link does provide interesting concepts, and I've seen it before, but it is for fixed point with a drastically finite range of values; a slew of conditional statements like that will not quite work well with floating point, it seems.

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.

si32 E_mathLog2if(sf32 x){ register ui32 c; c = _E_floatGetExponentf(x); if(!c){ c = _E_floatGetMantissaf(x); c |= (c >> 1); c |= (c >> 2); c |= (c >> 4); c |= (c >> 8); c |= (c >> 16); c >>= 1; c -= ((c >> 1) & 0x55555555); c = (((c >> 2) & 0x33333333) + (c & 0x33333333)); c = (((c >> 4) + c) & 0x0f0f0f0f); c += (c >> 8); c = (c + (c >> 16)) & 0x3f; if(!c) return 0; c = (ui32)(-(23 - c)); } return _E_floatGetSignf(x) ? ~0 : (si32)c - 127; }

That link does provide interesting concepts, and I've seen it before, but it is for fixed point with a drastically finite range of values; a slew of conditional statements like that will not quite work well with floating point, it seems.

**Edited by Ectara, 11 May 2012 - 05:09 PM.**

Posted 11 May 2012 - 05:26 PM

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:

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

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

**Edited by taby, 11 May 2012 - 05:40 PM.**

Posted 11 May 2012 - 06:00 PM

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.

Posted 11 May 2012 - 06:05 PM

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.

Posted 11 May 2012 - 06:17 PM

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.

Posted 11 May 2012 - 06:26 PM

For future reference, details like this are kind of a big deal.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.

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.

[ 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 ]

Posted 11 May 2012 - 07:00 PM

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.

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.

Posted 11 May 2012 - 07:38 PM

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:

Divide the value by the integer log2 of it leaving you with the decmial portion.

Double the value.

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.

**Edited by iMalc, 11 May 2012 - 07:40 PM.**

"In order to understand recursion, you must first understand recursion."

My website dedicated to sorting algorithms

My website dedicated to sorting algorithms