Behavior change for Vector2.Lerp and Vector4.Lerp

The implementation of Vector2.Lerp(Vector2, Vector2, Single) and Vector4.Lerp(Vector4, Vector4, Single) changed to correctly account for a floating-point rounding error.

Change description

Previously, Vector2.Lerp(Vector2, Vector2, Single) and Vector4.Lerp(Vector4, Vector4, Single) were implemented as value1 + (value2 - value1) * amount. However, due to a floating-point rounding error, this algorithm doesn't always return value2 when amount is 1.0f.

In .NET 5 and later, the implementation uses the same algorithm as Vector3.Lerp(Vector3, Vector3, Single), which is (value1 * (1.0f - amount)) + (value2 * amount). This algorithm correctly accounts for the rounding error. Now, when amount is 1.0f, the result is precisely value2. The updated algorithm also allows the algorithm to be freely optimized using MathF.FusedMultiplyAdd when it's available.

Version introduced

5.0

No action is necessary. However, if you want to maintain the old behavior, you can implement your own Lerp function that uses the previous algorithm of value1 + (value2 - value1) * amount.

Affected APIs