Floating point linear interpolation

As Jason C points out in the comments, the version you posted is most likely the best choice, due to its superior precision near the edge cases:

float lerp(float a, float b, float f)
{
    return a * (1.0 - f) + (b * f);
}

If we disregard from precision for a while, we can simplify the expression as follows:

    a(1 − f) × (ba)
 = aaf + bf
 = a + f(ba)

Which means we could write it like this:

float lerp(float a, float b, float f)
{
    return a + f * (b - a);
}

In this version we’ve gotten rid of one multiplication, but lost some precision.

Leave a Comment