ProwlEngine / Prowl

An Open Source C# 3D Game Engine under MIT license, inspired by Unity and featuring a complete editor
MIT License
447 stars 37 forks source link

Fix: never compare equality of floating numbers #157

Closed brmassa closed 2 months ago

brmassa commented 2 months ago

single and double 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 pure 90f, 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.

PaperPrototype commented 2 months ago

instead of a threshold maybe create a custom comparison check? That uses Application.FloatEqualThreshold

michaelsakharov commented 2 months ago

instead of a threshold maybe create a custom comparison check? That uses Application.FloatEqualThreshold

  • [ ] Math.FloatIsEqual(floatValue, comparedTo)
  • [ ] Math.FloatIsGreater(floatValue, comparedTo)
  • [ ] Math.FloatIsLesser(floatValue, comparedTo)

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.

brmassa commented 2 months ago

I agree with all comments:

brmassa commented 2 months ago

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