Vector2/4 Lerp behavior changed for .NET 5 #18937
Labels
breaking-change
Indicates a .NET Core breaking change
🏁 Release: .NET 5
Work items for the .NET 5 release
doc-idea
Indicates issues that are suggestions for new topics [org][type][category]
Vector2/3 Lerp behavior changed for .NET 5
Vector2.Lerp(value1, value2, amount)
andVector4.Lerp(value1, value2, amount)
were implemented asvalue1 + (value2 - value1) * amount
. However, due to floating-point rounding error this will not always returnvalue2
whenamount == 1.0f
.The implementation was updated to use the same algorithm as
Vector3.Lerp(value1, value2, amount)
which is(value1 * (1.0f - amount)) + (value2 * amount)
. This algorithm correctly accounts for the rounding error and also allows the algorithm to be freely optimized usingFusedMultiplyAdd
when available.Version introduced
.NET 5
Old behavior
For certain inputs, when
amount
was1.0f
the result would not be preciselyvalue2
.New behavior
When
amount
is1.0f
, the result will be preciselyvalue2
.Reason for change
The result would not be correctly interpolated as you could never get
value2
as a result for certain inputs.Recommended action
If maintaining the old behavior is desired, the user could implement their own
Lerp
function that uses the previous algorithm:value1 + (value2 - value1) * amount
Category
Core .NET libraries
Affected APIs
Vector2.Lerp(Vector2, Vector2, Single)
Vector4.Lerp(Vector4, Vector4, Single)
Issue metadata
The text was updated successfully, but these errors were encountered: