Please take a moment to fill out this survey on your gaming habits!

# Types/Fixed

## Fixed (Galaxy Type)

• Fixed is a 32-bit fixed (as opposed to floating point) number where 19 bits are available in the integer portion, 12 bits are available in the fractional portion and 1 bit is giving the sign.
• In the Galaxy Editor GUI fixed is referred to as a real.
• (231 - 1) / 4096 = 524287.999755859375 is the greatest fixed number.
• -(231 - 1) / 4096 = -524287.999755859375 is the least fixed number.
• 2-12 = 1 / 4096 = 0.000244140625, is the smallest non-zero fixed number.

### Galaxy Code

```fixed f = 1.0;
f = f / 4096.0;
f = f * 4096.0; // f == 1.0
```
• Note: There is a bug when using /= and *=
```fixed f;
```
```f = 1;
f *= 2;
// f = 0.0004882812
```
```f = 1;
f /= 2;
// f = 2048
```
```f = 1;
f /= 4;
// f = 1024
```

### Game File Type

#### fixed32

• Sign: 1 bit
• Integer part: 19 bits (524 288)
• Decimal part: 12 bits (4096)

#### fixed16

• Sign: 1 bit
• Integer part: 5 bits (16)
• Decimal part: 11 bits (2048)

#### fixed8

• Unsigned
• Integer part: 0 bit
• Decimal part: 8 bits (256)

### Miscellaneous

• Here is a useful function: FixedToString
• FixedToString(6.66666, 0); // 6
• FixedToString(6.66666, 1); // 6.6
• FixedToString(6.66666, 3); // 6.666
```string FixedToString(fixed f, int precision) {
string s = "";
int g;
```
```  if (precision < 0) {
precision = 0;
}
```
```  //Negative case
if (f < 0) {
s = "-";
f = -f;
}
```
```  // Integer part
g = FixedToInt(f);
s = s + IntToString(g);
f = f - g;
```
```  if (precision != 0 && f != 0) {
s = s + ".";
```
```    // Decimal part
do {
f = f * 10;
g = FixedToInt(f);
s = s + IntToString(g);
f = f - g;
precision = precision - 1;
} while (f != 0 && precision != 0);
}
```
```  return s;
}
```
• In order to know how many bits are associated to the integer and decimal part here is a handy function:
```void TestFixedBounds () {
int i = 2;
fixed f = 1;
```
```  // Decimal part
do {
i *= 2;
f = 1;
f = f / i;
d(IntToString(i) + ":" + IntToString(FixedToInt(f * i)));
} while (FixedToInt(f * i) == 1);
// ...
// 4096:1 : 2^12
// 8192:0
```
```  // Integer part
i = 2;
do {
i *= 2;
f = i;
d(IntToString(i) + ":" + IntToString(FixedToInt(f)));
} while (FixedToInt(f) == i);
// ...
// 262144:262144  : 2^19
// 524288:-524288
}
```
• To get the decimal bound, you just test if (f / 2n) * 2n = f and the integer bound is based on int(fixed: 2n) == int: 2n