algorithm - converting floating point to 32-bit fixed point in Java -


I have to change a floating point at a 32-bit fixed point in Java.

Understand what the 32-bit fixed point is?

Can anyone help with algorithms?

For a fixed point number integer part, by using a certain number of bits of one type, the actual number , And partial part of the remaining bits. The number of bits representing each part is fixed (hence the name, fixed point) An integer type is usually used to store fixed-point values.

Fixed-point numbers are commonly used in systems, which are not floating point support, or require more speed than floating point. Fixed point count can be done using the integer instructions of the CPU.

A 32-bit fixed-point number will be stored in 32-bit types like int .

Generally, each bit (unsigned in this case) integer type will represent the integer value 2 ^ n as follows:

  1 0 0 0 0 0 = 2 ^ 7 + 2 ^ 5 + 2 ^ 4 + 2 ^ 1 = 178 2 ^ 7 2 ^ 6 2 ^ 5 2 ^ 4 2 ^ 3 2 ^ 2 2 ^ 1 2 ^ 0  

But if type is used to store fixed point values, bits are interpreted in slightly different ways:

  1 0 1 0 0 0 = 2 ^ 3 + 2 ^ 1 + 2 ^ 0 + 2 ^ -3 = 11.125 2 ^ 3 2 ^ 2 2 ^ 1 2 ^ 0 2 ^ -1 ^ 2 ^ -2 ^ 2 ^ -4  

fixed point At the said 4.4 fixed point numbers in the example, because integer consists of 4 bits in the fractional part of 4 bits and part number. Fixed-point values ​​in a 32 bit type will usually be in the format 16.16, but it can be 24.8, 28.4 or any other combination.

Switching from a floating-point value to a fixed-point value involves following steps:

  1. Multiply float by 2 ^ (the number of variable bits for the type) Do, like For the 2.88 24.8
  2. If necessary, circle the result (add 0.5 only), and leave it the integer value (or enter the integer type).
  3. Fixed value type to this value.

Obviously you can lose some accuracy in partial part of the number. If the partial part is accurate, then the choice of fixed-point format can reflect this - like

negative points can be controlled in the same way, if your fixed-point number is signed needed.

If my Java was strong I'm attempting some code, but I usually write such things in C, so I will not attempt Java version Besides, Stacker The version looks good for me, with a slight exception, it does not offer the possibility of goal. He shows you how to multiply (change is important!)


Comments

Popular posts from this blog

Eclipse CDT variable colors in editor -

AJAX doesn't send POST query -

wpf - Custom Message Box Advice -