diff --git a/dbms/src/Functions/divide.cpp b/dbms/src/Functions/divide.cpp index 53c5de79448..8548fe4543f 100644 --- a/dbms/src/Functions/divide.cpp +++ b/dbms/src/Functions/divide.cpp @@ -62,7 +62,10 @@ struct TiDBDivideFloatingImpl template static Result apply(A a, B b) { - return static_cast(a) / b; + if constexpr (std::is_integral_v) + return (static_cast(a) + b / 2) / b; + else + return static_cast(a) / b; } template static Result apply(A a, B b, UInt8 & res_null) @@ -75,7 +78,7 @@ struct TiDBDivideFloatingImpl res_null = 1; return static_cast(0); } - return static_cast(a) / b; + return apply(a, b); } }; @@ -102,7 +105,7 @@ struct TiDBDivideFloatingImpl res_null = 1; return static_cast(0); } - return static_cast(a) / static_cast(b); + return apply(a, b); } }; @@ -332,4 +335,4 @@ void registerFunctionDivideIntegralOrZero(FunctionFactory & factory) factory.registerFunction(); } -} // namespace DB \ No newline at end of file +} // namespace DB