You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by mo...@apache.org on 2021/12/24 13:23:25 UTC

[incubator-doris] branch master updated: [fix](function) fix round function for inaccuracy (#7421)

This is an automated email from the ASF dual-hosted git repository.

morningman pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-doris.git


The following commit(s) were added to refs/heads/master by this push:
     new 3ba6dcf  [fix](function) fix round function for inaccuracy (#7421)
3ba6dcf is described below

commit 3ba6dcf236a57ed2d77ee39f9d5150851ba148dc
Author: shee <13...@users.noreply.github.com>
AuthorDate: Fri Dec 24 21:23:11 2021 +0800

    [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 28424f5..787887d 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 64d1f96..c9008d8 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";

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org