views:

94

answers:

4

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

Not able to understand what is a 32-bit fixed point ?

Can any body help with algorithm ?

+1  A: 

The definition of 32-bit fixed point could vary. The general idea of 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 a 32-bit one, the most common split is probably even (16 before, 16 after), but depending on the purpose there's no guarantee of that.

As far as the conversion goes, again it's open to some variation -- for example, if the input number is outside the range of the target, you might want to do any number of different things (e.g., in some cases wraparound could make sense, but in others saturation might be preferred).

Jerry Coffin
A: 

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

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

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

dan04
+1  A: 

A very simple example for converting to fixed point, it shows how to convert and multiplies PI by2. The resulting is converted back to double to demonstrate that the mantissa wasn't lost during calculation with integers.

You could expand that easily with sin() and cos() lookup tables etc. I would recommend if you plan to use fixed point to look 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 ));

    }
}

OUTPUT

f1:205887,3
f2:131072,2
r:411774,6
double: 6.283172607421875
stacker
+1 for the multiply method. Two suggestions for improvement though: offer rounding when converting `double` to fixed-point; and override `toString()` so a proper interpretation of the fixed-point value can be printed easily (which you could then use in your test).
MatthewD
+2  A: 

A fixed-point number is a representation of a real number using a certain number of bits of a type for the integer part, and the remaining bits of the type for the fractional part. 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 usually used in systems which don't have floating point support, or need more speed than floating point can provide. Fixed-point calculations can be performed using the CPU's integer instructions.

A 32-bit fixed-point number would be stored in an 32-bit type such as int.

Normally each bit in an (unsigned in this case) integer type would represent an integer value 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 a fixed-point value, the bits are interpreted slightly differently:

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

The fixed point number in the example above is called a 4.4 fixed-point number, since there are 4 bits in the integer part and 4 bits in the fractional part of the number. In a 32 bit type the fixed-point value would typically be in 16.16 format, but also could be 24.8, 28.4 or any other combination.

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

  1. Multiply the float by 2^(number of fractional bits for the type), eg. 2^8 for 24.8
  2. Round the result (just add 0.5) if necessary, and floor it (or cast to an integer type) leaving an integer value.
  3. Assign this value into the fixed-point type.

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

Negative values can also be handled in the same way if your fixed-point number needs to be signed.

If my Java were stronger I'd attempt some code, but I usually write such things in C, so I won't attempt a Java version. Besides, stacker's version looks good to me, with the minor exception that it doesn't offer the possibility of rounding. He even shows you how to perform a multiplication (the shift is important!)

MatthewD
+1 for spending so much time for such a 'unsung hero' topic.
stacker
All correct, and it would be better if you provided a solution so your answer can be accepted. Fortunately, I have that and I'm going to put it here: int number = Float.floatToRawIntBits(floatNumber);
instcode