-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Vector2/4.Lerp do not always return value2 when amount is 1 #35529
Comments
Tagging subscribers to this area: @tannergooding |
CC. @pgovind @jeffhandley, could we get approval for this breaking change? This difference was noticed here: #35525 (comment) |
Example: public static void Main(string[] args)
{
float v0 = 0.44728136f;
float v1 = 0.46345946f;
float t = 0.26402435f;
var lerp3 = Vector3.Lerp(new Vector3(v0), new Vector3(v1), t);
var lerp4 = Vector4.Lerp(new Vector4(v0), new Vector4(v1), t);
Console.WriteLine(lerp3);
Console.WriteLine(lerp4);
} Prints:
|
@tannergooding This seems good to me, assuming this gives us comprehensive consistency. Are there any other places where the same approach would apply? To move this forward, we should follow the model seen in a previous breaking change where we lay out the arguments for making the breaking change in 5.0 but not applying the change to down-versions. Please tag @marklio and @PriyaPurkayastha on that comment to get their approval. Their approval will suffice for getting this merged in. |
There are likely various cases where floating-point algorithms could be tweaked to provide correct results but I don't believe there is an easy way to find them. In this case, |
As per the title...
We wouldn't bring this for 3.x because:
We want to fix this in 5.0 because:
CC. @marklio and @PriyaPurkayastha for approval. If approved, I'll mark this as |
@tannergooding any idea why we have not seen customer reports on these errors? It appears that Vector*.Lerp has been around since .NET Core 1.0/.NET 4.6. You have mentioned that linear interpolation is a well known scenario impacted by this - does that mean that there was customer feedback as a result of which this has been fixed for Vector3? Vector3 fix was probably in a recent Preview and hence too early for feedback on the fix right? |
Yes, the algorithm for correct linear interpolation which accounts for rounding error is "well known" and you can find it documented on Wikipedia, many StackOverflow posts, other math libraries, etc.
That is correct.
apisof.net currently indicates 0% usage, which doesn't quite seem right (@terrajobst ?):
The change if users are expecting the existing behavior is rather trivial as they can just use |
Unfortunately, apisof.net is notoriously bad for making these kinds of decisions. We're working on democratizing some more helpful data. For now, overall usage of these types is low. Here's the number of NuGet package ids using Lerp from each of the vector* types:
These numbers are pretty low, and we're not breaking them significantly. I agree with @PriyaPurkayastha that this is a fine change for a major release, and we should document it for completeness. My understanding is that they will simply be getting a more accurate result, right? We have precedent for doing that in the JIT without too much concern for compat (we can easily change things that hoist floating point operations to higher precision operations that may yield more accurate results). |
Correct, we would now return exactly |
I've marked this as |
Can I take this? |
Assigned out, thanks @john-h-k ! |
Breaking change doc was logged here: dotnet/docs#18937 |
Vector2.Lerp(value1, value2, amount)
andVector4.Lerp(value1, value2, amount)
are currently implemented asvalue1 + (value2 - value1) * amount
. However, due to floating-point rounding error this will not always returnvalue2
whenamount == 1.0f
.These should be 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.The text was updated successfully, but these errors were encountered: