float: fix negative-zero/zero almost_equal

This commit is contained in:
Danny Robson 2015-01-29 15:44:57 +11:00
parent 794a688470
commit c841aadda0
2 changed files with 8 additions and 0 deletions

View File

@ -126,6 +126,12 @@ ieee_float<E, S>::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;
}

View File

@ -23,6 +23,8 @@ main (int, char **) {
CHECK (!almost_equal (numeric_limits<double>::quiet_NaN (),
numeric_limits<double>::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);