Commit c5e312a7 authored by Christoph Groth's avatar Christoph Groth
Browse files

make round agree strictly with numpy (round to even)

parent 111a3678
......@@ -494,9 +494,25 @@ struct Round<Kind, Complex> {
Complex operator()(Complex) { return 0.0/0.0; }
};
// The following types are to be used as Kind template parameter for Round.
struct Nearest { double operator()(double x) { return std::round(x); } };
// The following three types are used as Kind template parameter for Round.
struct Nearest {
// Rounding to nearest even, same as numpy.
double operator()(double x) {
double y = std::floor(x), r = x - y;
if (r > 0.5) {
++y;
} else if (r == 0.5) {
r = y - 2.0 * std::floor(0.5 * y);
if (r == 1) ++y;
}
if (y == 0 && x < 0) y = -0.0;
return y;
}
};
struct Floor { double operator()(double x) { return std::floor(x); } };
struct Ceil { double operator()(double x) { return std::ceil(x); } };
template <typename T>
......
......@@ -270,3 +270,7 @@ def test_unary_ufuncs():
assert_raises(TypeError, ta_func, a.tolist())
else:
assert_equal(ta_func(a.tolist()), np_func(a))
for x in [-987654322.5, -987654321.5, -4.51, -3.51, -2.5, -2.0,
-1.7, -1.5, -0.5, -0.3, -0.0, 0.0, 0.3, 0.5, 1.5, 1.7,
2.0, 2.5, 3.51, 4.51, 987654321.5, 987654322.5]:
assert_equal(ta_func(x), np_func(x))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment