For the complete documentation index, see llms.txt. This page is also available as Markdown.

Fixed-point calculations

Represent decimals with a documented scale:

fixed-point-scale.gpc
define SCALE = 1000;

int gain = 1250; // 1.250
int input;
int output;

main {
    input = get_ival(PS5_RX);
    output = (input * gain) / SCALE;
    set_val(PS5_RX, bounded_axis(output));
}

function bounded_axis(value) {
    if(value < -100) return -100;
    if(value > 100) return 100;
    return value;
}

Guidelines:

  • Multiply before divide to preserve fractional precision.

  • Check multiplication for 32-bit overflow.

  • State whether negative division/truncation is acceptable.

  • Clamp at the boundary where a narrower device range is required.

  • Do not repeatedly rescale the same value through several stages.

Last updated