Fixed type (ACS) - DavidPH/GDCC GitHub Wiki

GDCC provides a proper fixed point type as an extension to ACS. By default, the fixed type and literal are simply an alias to int, and work identically to ACS.

With a pragma, the fixed type will behave as it's own type, and fixed point literals will also be of said type.

function void FixedTest (void)
{
   // To enable the fixed type and make fixed-point literals have fixed type, a
   // pragma is needed. If used inside of a function like this, it will apply
   // only inside the function. If used outside any function, it applies to the
   // entire file after its use.
   #pragma fixed on

   // Variables with fixed type are declared as normal. Without the effect of
   // the pragma, fixed is an alias for int.
   fixed x;

   // Similarly, without the pragma, fixed-point literals have int type.
   x = 12.5;

   // This will result in y being set to 6.25, FixedMul (and in division FixedDiv) is automatically called in fixed multiplication.
   fixed y = x * 0.5;

   // And this will result in i being set to 6, the type conversion being
   // performed implicitly.
   int i = y;

   // Similarly, this will result in x being set to 6.0.
   x = i;

   // When mixing int and fixed in an expression, the int expression will be
   // converted to fixed type for the operation.
   x = y + i; // x = 12.25

   // Type conversion can be performed explicitly using the C type cast syntax.
   y = (fixed)i / 8; // y = 0.75

   // If the fixed pragma is enabled only after including zcommon.acs, it is not
   // possible to print actual fixed values. Attempting to do so will result in
   // the value being converted and misinterpreted.
   //Log(s: "A fixed number: ", f: x);
}