# Approximation Floating Point Numbers with Integers

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

## Recommended Posts

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!

##### Share on other sites
if you have a floating point number between -1.0 and 1.0:int num = (int)(128.0f * (fpNum + 1.0));

##### Share on other sites
ok thats a little bit embarrassing - i thought it would be more complicated than that. Thanks for your help, solution sounds good to me

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

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

1. 1
Rutin
40
2. 2
3. 3
4. 4
5. 5

• 18
• 19
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633362
• Total Posts
3011531
• ### Who's Online (See full list)

There are no registered users currently online

×