Normalize to Range and Inverse Lerp

Is there a functional difference between Normalizing to Range and Inverse Lerp?

If used in “regular” conditions (i.e. values for A and B, Min and Max far apart enough and properly ordered) they produce the same result. However when A and B are very close/identical or Min and Max are identical, they behave differently. Look at the respective source codes:


float UKismetMathLibrary::NormalizeToRange(float Value, float RangeMin, float RangeMax)
{
    if (RangeMin == RangeMax)
    {
        return RangeMin;
    }

    if (RangeMin > RangeMax)
    {
        Swap(RangeMin, RangeMax);
    }
    return (Value - RangeMin) / (RangeMax - RangeMin);
}


float UKismetMathLibrary::InverseLerp(float A, float B, float Value)
{
    if (FMath::IsNearlyEqual(A, B))
    {
        if (Value < A)
        {
            return 0;
        }
        else
        {
            return 1;
        }
    }
    else
    {
        return ((Value - A) / (B - A));
    }
}

1 Like

Note that this is no longer the case. The body of NormalizeToRange looks more like InverseLerp (but retains the swap), and InverseLerp has been deprecated.