Map Numeric Ranges

A picture of the author, Jakob Maier
Jakob Maier
Oct 26, 2023

Map Numeric Ranges

To map any range [in_min, in_max] to [out_min, out_max] we can use this function:

float map_range(float s, float in_min, float in_max, float out_min, float out_max)
{
    assert(in_min <= s && s <= in_max);
    return out_min + (s - in_min) * (out_max - out_min) / (in_max - in_min);
}

There are a few special cases where we can simplify this:

Map from [0, 1] to [out_min, out_max]:

float map_range(float s, float out_min, float out_max)
{
    return out_min + s * (out_max - out_min);
}

Map from [-1, 1] to [0, 1]:

float map_range(float s)
{
    assert(-1.0f <= s && s <= 1.0f);
    return (s + 1.0f) / 2.0f;
}

Map from [-0.5, 0.5] to [0, 1]:

float map_range(float s)
{
    assert(-0.5f <= s && s <= 0.5f);
    return s + 0.5f;
}

Map from [0, 1] to [-1, 1]:

float map_range(float s)
{
    assert(0.0f <= s && s <= 1.0f);
    return s * 2.0f - 1.0f;
}

↑ back to top

© 2023 Jakob Maier
kontakt & impressum
edit