Convert floating point to 32 bit fixed point in Java

I need to convert floating point to 32 bit fixed point in Java.

Can't figure out what a 32-bit fixed point is?

Can any body help with the algorithm?

+7
java math algorithm compression
source share
4 answers

A fixed-point number is a representation of a real number using a certain number of type bits for the integer part, and the remaining type bits for the fractional part. The number of bits representing each part is fixed (hence the name, fixed point). An integer type is commonly used to store fixed-point values.

Fixed-point numbers are commonly used on systems that do not support floating point, or require more speed than a floating point can provide. Fixed point calculations can be performed using integer CPU instructions.

A 32-bit fixed-point number will be stored in a 32-bit type, such as int .

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

  1 0 1 1 0 0 1 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 the type is used to store the value of a fixed point, the bits are interpreted somewhat differently:

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

The fixed point number in the above example is called the fixed point number 4.4, since there are 4 bits in the binary part and 4 bits in the fractional part of the number. In the 32-bit type, the fixed-point value will usually be in the 16.16 format, but it can also be 24.8, 28.4, or any other combination.

Converting from a floating point value to a fixed point value includes the following steps:

  • Multiply the float by 2 ^ (the number of fractional bits for the type), for example. 2 ^ 8 for 24.8
  • If necessary, round the result (just add 0.5) and place it (or drop it to an integer type), leaving the integer value.
  • Assign this value to a fixed point type.

Obviously, you may lose some precision in the fractional part of the number. If the accuracy of the fractional part is important, the choice of fixed-point format may reflect this - for example. use 16.16 or 8.24 instead of 24.8.

Negative values ​​can also be processed the same way if you need to specify a fixed point number.

If my Java were stronger, I would try some code, but usually I write such things in C, so I will not try to use the Java version. Also, the stacker version looks good to me, with the slight exception that it does not offer rounding capabilities. It even shows you how to do multiplication (shift is important!)

+16
source share

A very simple example of converting to a fixed point, it shows how to convert and multiply PI by 2. The result is converted back to double to demonstrate that the mantissa was not lost when calculating with integers.

You can easily expand this with the lookup tables sin () and cos (), etc. I would recommend if you plan to use a fixed point to search for a java fixed point library.

 public class Fix { public static final int FIXED_POINT = 16; public static final int ONE = 1 << FIXED_POINT; public static int mul(int a, int b) { return (int) ((long) a * (long) b >> FIXED_POINT); } public static int toFix( double val ) { return (int) (val * ONE); } public static int intVal( int fix ) { return fix >> FIXED_POINT; } public static double doubleVal( int fix ) { return ((double) fix) / ONE; } public static void main(String[] args) { int f1 = toFix( Math.PI ); int f2 = toFix( 2 ); int result = mul( f1, f2 ); System.out.println( "f1:" + f1 + "," + intVal( f1 ) ); System.out.println( "f2:" + f2 + "," + intVal( f2 ) ); System.out.println( "r:" + result +"," + intVal( result)); System.out.println( "double: " + doubleVal( result )); } } 

EXIT

 f1:205887,3 f2:131072,2 r:411774,6 double: 6.283172607421875 
+3
source share

The definition of a 32-bit fixed point may vary. The general idea of ​​a fixed point is that you have some fixed number of bits before and another fixed number of bits after the decimal point (or binary point). For 32-bit, the most common split is probably even (16 before, 16 after), but depending on the purpose there is no guarantee.

As for the conversion, it opens again for some change - for example, if the input number is outside the target object, you may want to do any number of different things (for example, in some cases it might make sense to wrap, but in others saturation may be preferred).

+1
source share

A fixed-point type is a number that has a fixed number of decimal / binary places after the number notation. Or, in a more general sense, a type that can store multiples of 1 / N for some positive integer N.

Internally, fixed point numbers are stored as a value times the scaling factor. For example, 123.45 with a scale factor of 100 is stored as if it were an integer of 12345.

To convert the internal value of a fixed-point number to a floating-point, simply divide it by the scaling factor. To convert another method, multiply by the scaling factor and round to the nearest integer.

+1
source share

All Articles