1

Using Unity. I need to used fixed point to network some data deterministically. How do I convert a fixed point back into a float so that I can apply it to the objects transform?

public class Fixed
{
    private int front;
    private int back;

    private const uint MAX_32 = 4294967295;
}

This is the class I have for the fixed point number.

gunr2171
  • 16,104
  • 25
  • 61
  • 88

3 Answers3

0

Don't overthink it. Just scale your real number by a constant amount (ideally a power of two or ten, depending on your needs) and store in an integer. To convert back, divide by the same amount. In your case, consider multiplying the floating-point value by 4294967296.0 and assigning to a long.

John McFarlane
  • 5,528
  • 4
  • 34
  • 38
0

As @NineBerry said, you can use the type Decimal to represent fixed point numbers:

    decimal fixedPointNum = 123.456m; // a fixed point number
    float floatNum = Convert.ToSingle(fixedPointNum); 
    Console.WriteLine(floatNum);
  • 2
    You could inform yourself what a fixed-point number is. The aim is to simulate real numbers in a very restricted subset using only a small number of integer operations. This is quite the opposite to a multi-precision floating-point format, regardless of if that is implemented using 2 or 10 (or some power of them) as base. – Lutz Lehmann Apr 23 '23 at 08:38
0

The answer depends on the range. If you are trying to represent the decimal numbers from (-1, +1) you could divide the input by 0x7FFFFFFF.

If you have some other range (let us say -256.0, 255.0) you could divide the input by 0x007FFFFF

It all depends what ranges are you expecting in your inputs