float: fix negative-zero/zero almost_equal
This commit is contained in:
parent
794a688470
commit
c841aadda0
@ -126,6 +126,12 @@ ieee_float<E, S>::almost_equal (floating_t _a,
|
|||||||
if (a.s == b.s)
|
if (a.s == b.s)
|
||||||
return true;
|
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);
|
uint_t diff = std::abs (a.s - b.s);
|
||||||
return diff <= ulps;
|
return diff <= ulps;
|
||||||
}
|
}
|
||||||
|
@ -23,6 +23,8 @@ main (int, char **) {
|
|||||||
CHECK (!almost_equal (numeric_limits<double>::quiet_NaN (),
|
CHECK (!almost_equal (numeric_limits<double>::quiet_NaN (),
|
||||||
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 (min (-2, 0, 2), -2);
|
||||||
CHECK_EQ (max (-2, 0, 2), 2);
|
CHECK_EQ (max (-2, 0, 2), 2);
|
||||||
|
Loading…
Reference in New Issue
Block a user