# Floating point base 2 logarithm

This topic is 2347 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.

##### Share on other sites
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.

##### Share on other sites
Try this:

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

##### Share on other sites
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. Edited by taby

##### Share on other sites
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? Edited by Ectara

##### Share on other sites

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

##### Share on other sites
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.

##### Share on other sites

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

##### Share on other sites

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

##### Share on other sites
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.
 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

1. 1
2. 2
3. 3
4. 4
Rutin
17
5. 5

• 14
• 9
• 10
• 12
• 17
• ### Forum Statistics

• Total Topics
632907
• Total Posts
3009164
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!