Skip to content

Vector2/4 Lerp behavior changed for .NET 5 #18937

@tannergooding

Description

@tannergooding

Vector2/3 Lerp behavior changed for .NET 5

Vector2.Lerp(value1, value2, amount) and Vector4.Lerp(value1, value2, amount) were implemented as value1 + (value2 - value1) * amount. However, due to floating-point rounding error this will not always return value2 when amount == 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 using FusedMultiplyAdd when available.

Version introduced

.NET 5

Old behavior

For certain inputs, when amount was 1.0f the result would not be precisely value2.

New behavior

When amount is 1.0f, the result will be precisely value2.

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

  • Issue type: breaking-change

Metadata

Metadata

Assignees

Labels

🏁 Release: .NET 5Work items for the .NET 5 releasebreaking-changeIndicates a .NET Core breaking change

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions