Approximation Floating Point Numbers with Integers

Started by
3 comments, last by Sik_the_hedgehog 13 years ago
Unfortunately i haven't found any valueable information on this topic using google. So maybe you can help me:

I am searching for some information how floating point (or fixed point) numbers can be aproximated with integer numbers. So for example, if i know the floating point number is only between -1 and +1, how can i reasonably approximate this number with a 8bit integer number (maybe bitshifting it or something like that)? I am either interested in theoretical aspecs as well as real implementations.

I Hope i dont sound too weird asking such things. Thanks!
Advertisement
if you have a floating point number between -1.0 and 1.0:int num = (int)(128.0f * (fpNum + 1.0));
ok thats a little bit embarrassing - i thought it would be more complicated than that. Thanks for your help, solution sounds good to me
The linear mapping that BuffaloJ gave would be my first answer too. However, a slight generalization might be useful in some situations, in order to allow certain regions of the interval to have more resolution than others. It's this:

* You have floating-point numbers that can fall in the interval [a,b].
* You have an unsigned integer type that can take values between 0 and N; for an n-bit number, N=2[sup]n[/sup]-1.
* What you're looking for is a function f from the interval [a,b] to the interval [0,N] satisfying,
- f(a)=0 and f(b) = N
- f is strictly increasing.

An example of such a function is the linear mapping,

f(x) = N*[ (x-a)/(b-a) ]

like what BuffaloJ gave you; this will give you uniform sampling, but other choices are possible as well. E.g.,

f(x) = N*[ (x-a)/(b-a) ][sup]2[/sup]

will dedicate more resolution to numbers at the top of the range, and less to those at the bottom,

f(x) = N*[ (x-a)/(b-a) ][sup]1/2[/sup]

will dedicate more to the bottom, and

f(x) = N*([2*(x-a)/(b-a) - 1][sup]1/3[/sup] + 1)/2

will concentrate more around the middle.

The sampling density around some floating point number 'x' is approximately the value of the derivative, df/dx(x). You get more resolution where 'f' is steep.
Just so you know, fixed point is literally approximating non-integer values using integers. For example, with 16.16 fixed point (16 bits integer, 16 bits fraction), the value 1.0 is stored as 65536.
Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

This topic is closed to new replies.

Advertisement