diff --git a/float.cpp b/float.cpp index a49da312..691cfa1f 100644 --- a/float.cpp +++ b/float.cpp @@ -126,6 +126,12 @@ ieee_float::almost_equal (floating_t _a, if (a.s == b.s) return true; + // Order as twos complement to avoid negative zero/zero comparison issues + if (a.s < 0) + a.s = (1L << (TOTAL_BITS - 1)) - a.s; + if (b.s < 0) + b.s = (1L << (TOTAL_BITS - 1)) - b.s; + uint_t diff = std::abs (a.s - b.s); return diff <= ulps; } diff --git a/test/maths/maths.cpp b/test/maths/maths.cpp index 3df7a8b4..b43e38a9 100644 --- a/test/maths/maths.cpp +++ b/test/maths/maths.cpp @@ -23,6 +23,8 @@ main (int, char **) { CHECK (!almost_equal (numeric_limits::quiet_NaN (), numeric_limits::quiet_NaN ())); + CHECK (almost_equal (0.f, -0.f)); + CHECK (almost_equal (0., -0.)); CHECK_EQ (min (-2, 0, 2), -2); CHECK_EQ (max (-2, 0, 2), 2);