Addint based floating point approximate comparison
authorvaron <varon@users.noreply.github.com>
Sun, 19 Mar 2017 13:33:58 +0000 (15:33 +0200)
committervaron <varon@users.noreply.github.com>
Sun, 19 Mar 2017 13:34:30 +0000 (15:34 +0200)
src/OpenTK/Math/MathHelper.cs

index 81ad33b..a5ecc61 100644 (file)
@@ -326,8 +326,35 @@ namespace OpenTK
             return Math.Max(Math.Min(n, max), min);
         }
 
-        #endregion
-
-        #endregion
-    }
+               private static unsafe int FloatToInt32Bits(float f) {
+                       return *((int*) &f);
+               }
+
+               /// <summary>
+               /// Approximates floating point equality with a maximum number of different bits.
+               /// This is typically used in place of an epsilon comparison.
+               /// see: https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
+               /// see: https://stackoverflow.com/questions/3874627/floating-point-comparison-functions-for-c-sharp
+               /// </summary>
+               /// <param name="a">the first value to compare</param>
+               /// <param name="b">>the second value to compare</param>
+               /// <param name="maxDeltaBits">the number of floating point bits to check</param>
+               /// <returns></returns>
+               public static bool ApproximatelyEqual(float a, float b, int maxDeltaBits) {
+                       int aInt = FloatToInt32Bits(a);
+                       if (aInt < 0)
+                               aInt = Int32.MinValue - aInt;
+
+                       int bInt = FloatToInt32Bits(b);
+                       if (bInt < 0)
+                               bInt = Int32.MinValue - bInt;
+
+                       int intDiff = Math.Abs(aInt - bInt);
+                       return intDiff <= (1 << maxDeltaBits);
+               }
+
+               #endregion
+
+               #endregion
+       }
 }