From 3e38074a9f49fae439e9e8ce84d3f5da537d88c7 Mon Sep 17 00:00:00 2001 From: varon Date: Sun, 19 Mar 2017 15:56:16 +0200 Subject: [PATCH] Use longs to avoid two's complement failure in approxEqual --- src/OpenTK/Math/MathHelper.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/OpenTK/Math/MathHelper.cs b/src/OpenTK/Math/MathHelper.cs index a5ecc61c..f1effe6e 100644 --- a/src/OpenTK/Math/MathHelper.cs +++ b/src/OpenTK/Math/MathHelper.cs @@ -341,15 +341,16 @@ namespace OpenTK /// the number of floating point bits to check /// public static bool ApproximatelyEqual(float a, float b, int maxDeltaBits) { - int aInt = FloatToInt32Bits(a); + // we use longs here, otherwise we run into a two's complement problem, causing this to fail with -2 and 2.0 + long aInt = FloatToInt32Bits(a); if (aInt < 0) aInt = Int32.MinValue - aInt; - int bInt = FloatToInt32Bits(b); + long bInt = FloatToInt32Bits(b); if (bInt < 0) bInt = Int32.MinValue - bInt; - int intDiff = Math.Abs(aInt - bInt); + long intDiff = Math.Abs(aInt - bInt); return intDiff <= (1 << maxDeltaBits); }