From 3ba6dcf236a57ed2d77ee39f9d5150851ba148dc Mon Sep 17 00:00:00 2001 From: shee <13843187+qzsee@users.noreply.github.com> Date: Fri, 24 Dec 2021 21:23:11 +0800 Subject: [PATCH] [fix](function) fix round function for inaccuracy (#7421) --- be/src/exprs/math_functions.cpp | 2 +- be/test/exprs/math_functions_test.cpp | 31 ++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/be/src/exprs/math_functions.cpp b/be/src/exprs/math_functions.cpp index 28424f575d..787887d1fd 100644 --- a/be/src/exprs/math_functions.cpp +++ b/be/src/exprs/math_functions.cpp @@ -90,7 +90,7 @@ double MathFunctions::my_double_round(double value, int64_t dec, bool dec_unsign tmp2 = dec < 0 ? std::ceil(value_div_tmp) * tmp : std::ceil(value_mul_tmp) / tmp; } } else { - tmp2 = dec < 0 ? std::rint(value_div_tmp) * tmp : std::rint(value_mul_tmp) / tmp; + tmp2 = dec < 0 ? std::round(value_div_tmp) * tmp : std::round(value_mul_tmp) / tmp; } return tmp2; diff --git a/be/test/exprs/math_functions_test.cpp b/be/test/exprs/math_functions_test.cpp index 64d1f96722..c9008d89ad 100644 --- a/be/test/exprs/math_functions_test.cpp +++ b/be/test/exprs/math_functions_test.cpp @@ -262,7 +262,36 @@ TEST_F(MathFunctionsTest, unhex) { delete context; } -} // namespace doris + +TEST_F(MathFunctionsTest, round_up_to) { + + DoubleVal r0(0); + DoubleVal r1(1); + DoubleVal r2(3); + DoubleVal r3(4); + DoubleVal r4(3.5); + DoubleVal r5(3.55); + + DoubleVal r6(222500); + + ASSERT_EQ(r0, MathFunctions::round_up_to(ctx, DoubleVal(0), IntVal(0))); + ASSERT_EQ(r1, MathFunctions::round_up_to(ctx, DoubleVal(0.5), IntVal(0))); + ASSERT_EQ(r1, MathFunctions::round_up_to(ctx, DoubleVal(0.51), IntVal(0))); + // not 2 + ASSERT_EQ(r2, MathFunctions::round_up_to(ctx, DoubleVal(2.5), IntVal(0))); + ASSERT_EQ(r3, MathFunctions::round_up_to(ctx, DoubleVal(3.5), IntVal(0))); + + ASSERT_EQ(r4, MathFunctions::round_up_to(ctx, DoubleVal(3.5451), IntVal(1))); + ASSERT_EQ(r5, MathFunctions::round_up_to(ctx, DoubleVal(3.5451), IntVal(2))); + + // not 3.54 + ASSERT_EQ(r5, MathFunctions::round_up_to(ctx, DoubleVal(3.5450), IntVal(2))); + + // not 222400 + ASSERT_EQ(r6, MathFunctions::round_up_to(ctx, DoubleVal(222450.00), IntVal(-2))); +} + +}// namespace doris int main(int argc, char** argv) { std::string conffile = std::string(getenv("DORIS_HOME")) + "/conf/be.conf";