-
Notifications
You must be signed in to change notification settings - Fork 46
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
Fix: never compare equality of floating numbers #157
Fix: never compare equality of floating numbers #157
Conversation
@@ -48,7 +49,7 @@ public unsafe bool ConfigureContactManifold<TManifold>(int workerIndex, Collidab | |||
var b = CollidableMaterials[pair.B]; | |||
pairMaterial.FrictionCoefficient = a.FrictionCoefficient * b.FrictionCoefficient; | |||
pairMaterial.MaximumRecoveryVelocity = (float)MathD.Max(a.MaximumRecoveryVelocity, b.MaximumRecoveryVelocity); | |||
pairMaterial.SpringSettings = pairMaterial.MaximumRecoveryVelocity == a.MaximumRecoveryVelocity ? a.SpringSettings : b.SpringSettings; | |||
pairMaterial.SpringSettings = Math.Abs(pairMaterial.MaximumRecoveryVelocity - a.MaximumRecoveryVelocity) < Application.FloatEqualThreshold ? a.SpringSettings : b.SpringSettings; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I imagine in the case of BepuPhysics this wouldn't be ideal due to possible performance costs it would come with.
This is very frequently called, and it wouldn't matter much if there are errors due to floating precision here.
Other than that this seems like a good change to make.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
However, now that I look more closely at the rest of the method, there's a hefty float/double cast and MathD.Max being used. So maybe it doesn't matter as much as I thought.
instead of a threshold maybe create a custom comparison check? That uses
|
This is a good point Unity has Mathf.ApproximatelyEquals, Since our goal is to sorta mimic Unity's API to make transitioning as seamless as possible, it makes sense to recreate that idea. |
I agree with all comments:
|
I've created Mathf.ApproximatelyEquals because it's the same method name used for MathD. Also, I replaced several Epsilon comparisons with the Mathf.Epsilon and MathD.Epsilon |
single
anddouble
floats use an especial arithmetic that uses a trade-off of being super big/small, but not very precise.Eventually, 100f - 10f might result in
90.000000001
instead of pure90f
, so when comparing if 2 floats are the same, we need actually to check if they are close enough. Doing the pure comparison might result in weird behaviors.0.0001f
)