From e5d3742acd23a38fa03abf3040ddf8713a0ede17 Mon Sep 17 00:00:00 2001 From: Niranjan Artal <50492963+nartal1@users.noreply.github.com> Date: Wed, 16 Dec 2020 19:07:25 -0800 Subject: [PATCH] Fix round operator's HALF_EVEN computation for negative integers(#7014) Found a small bug while working on https://github.com/NVIDIA/spark-rapids/pull/1244/. For negative integers, it was not rounding to nearest even number. Authors: - Niranjan Artal - Conor Hoekstra Approvers: - Conor Hoekstra - Mark Harris URL: https://github.com/rapidsai/cudf/pull/7014 --- cpp/src/round/round.cu | 3 ++- cpp/tests/round/round_tests.cpp | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/cpp/src/round/round.cu b/cpp/src/round/round.cu index 9840a6a1e3d..90d183a6eef 100644 --- a/cpp/src/round/round.cu +++ b/cpp/src/round/round.cu @@ -181,7 +181,8 @@ struct half_even_negative { auto const down_over_n = e / n; // use this to determine HALF_EVEN case auto const down = down_over_n * n; // result from rounding down auto const diff = generic_abs(e - down); - auto const adjustment = (diff > n / 2) or (diff == n / 2 && down_over_n % 2 == 1) ? n : 0; + auto const adjustment = + (diff > n / 2) or (diff == n / 2 && generic_abs(down_over_n) % 2 == 1) ? n : 0; return down + generic_sign(e) * adjustment; } }; diff --git a/cpp/tests/round/round_tests.cpp b/cpp/tests/round/round_tests.cpp index d265900ef67..06fd5faddf0 100644 --- a/cpp/tests/round/round_tests.cpp +++ b/cpp/tests/round/round_tests.cpp @@ -503,6 +503,17 @@ TYPED_TEST(RoundTestsIntegerTypes, SimpleNegativeIntegerHalfUp) CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view()); } +TYPED_TEST(RoundTestsIntegerTypes, SimpleNegativeIntegerHalfEven) +{ + using fw_wrapper = cudf::test::fixed_width_column_wrapper; + + auto const input = fw_wrapper{-12, -135, -145, -146, -1454, -1455, -1500}; + auto const expected = fw_wrapper{-10, -140, -140, -150, -1450, -1460, -1500}; + auto const result = cudf::round(input, -1, cudf::rounding_method::HALF_EVEN); + + CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view()); +} + TEST_F(RoundTests, SimpleNegativeIntegerWithUnsignedNumbersHalfUp) { using fw_wrapper = cudf::test::fixed_width_column_wrapper;