Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix the issue that decimal divide not round. #6471

Merged
merged 32 commits into from
Feb 9, 2023
Merged
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
f2f7654
fix
LittleFall Dec 8, 2022
2a529e5
add test
LittleFall Dec 12, 2022
dbb471e
add more tests and fix bug
LittleFall Dec 13, 2022
2c7961a
Merge branch 'master' into fix/decimal-divide-round
LittleFall Dec 13, 2022
87cc360
Merge branch 'master' into fix/decimal-divide-round
LittleFall Jan 13, 2023
496d3ad
Merge branch 'master' into fix/decimal-divide-round
LittleFall Jan 17, 2023
65056ce
Merge remote-tracking branch 'origin/master' into fix/decimal-divide-…
LittleFall Jan 18, 2023
609b152
Merge branch 'master' into fix/decimal-divide-round
LittleFall Feb 7, 2023
05fbb66
test stage
LittleFall Feb 7, 2023
ff58c5c
Merge remote-tracking branch 'origin/fix/decimal-divide-round' into f…
LittleFall Feb 7, 2023
b175422
basic impl
LittleFall Feb 8, 2023
c2068b4
refine comment
LittleFall Feb 8, 2023
90df89b
tweaking
LittleFall Feb 8, 2023
65a8533
Merge branch 'master' into fix/decimal-divide-round
wuhuizuo Feb 8, 2023
1805d3d
more comment
LittleFall Feb 8, 2023
6eecf77
Merge remote-tracking branch 'origin/fix/decimal-divide-round' into f…
LittleFall Feb 8, 2023
2369f9a
add fullstack test
LittleFall Feb 8, 2023
94f621f
Merge branch 'master' into fix/decimal-divide-round
LittleFall Feb 8, 2023
d78ad7b
Merge branch 'master' into fix/decimal-divide-round
bestwoody Feb 8, 2023
f97053f
Merge branch 'master' into fix/decimal-divide-round
bestwoody Feb 8, 2023
5b94bd0
add comments
LittleFall Feb 9, 2023
ad26758
Merge branch 'master' into fix/decimal-divide-round
LittleFall Feb 9, 2023
655bc16
Merge remote-tracking branch 'origin/fix/decimal-divide-round' into f…
LittleFall Feb 9, 2023
eb005bc
EOL on EOF
LittleFall Feb 9, 2023
4ca7d30
tweaking
LittleFall Feb 9, 2023
2f460ec
Merge branch 'master' into fix/decimal-divide-round
ti-chi-bot Feb 9, 2023
7686a93
fix test
LittleFall Feb 9, 2023
04012f1
Merge remote-tracking branch 'origin/fix/decimal-divide-round' into f…
LittleFall Feb 9, 2023
62db0b3
Merge branch 'master' into fix/decimal-divide-round
ti-chi-bot Feb 9, 2023
0219214
disable issue_1425.test
LittleFall Feb 9, 2023
153ea48
Merge remote-tracking branch 'origin/fix/decimal-divide-round' into f…
LittleFall Feb 9, 2023
f1a9064
refine test
LittleFall Feb 9, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add comments
  • Loading branch information
LittleFall committed Feb 9, 2023
commit 5b94bd027dedabbb7203a225b0a1f9e170d0cc6e
6 changes: 6 additions & 0 deletions dbms/src/Functions/divide.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,20 @@ struct TiDBDivideFloatingImpl<A, B, false>
/// basically refer to https://stackoverflow.com/a/71634489
if constexpr (std::is_integral_v<Result> || std::is_same_v<Result, Int256>)
{
/// 1. do division first, get the quotient and mod, todo:(perf) find a unified `divmod` function to speed up this.
Result quotient = x / d;
Result mod = x % d;
/// 2. get the half of divisor, which is threshold to decide whether to round up or down.
/// note: don't directly use bit operation here, because it may cause unexpected result.
Result half = (d / 2) + (d % 2);

/// 3. compare the abstract values of mod and half, if mod >= half, then round up.
Result abs_m = mod < 0 ? -mod : mod;
Result abs_h = half < 0 ? -half : half;
if (abs_m >= abs_h)
{
/// 4. now we need to round up, i.e., add 1 to the quotient's absolute value.
/// if the signs of dividend and divisor are the same, then the quotient should be positive, otherwise negative.
if ((x < 0) == (d < 0)) // same_sign, i.e., quotient >= 0
quotient = quotient + 1;
else
Expand Down