Fix mod when the divisor is slightly greater than the half of dividend (#312) (#317)

This commit is contained in:
zlasd 2024-01-10 09:08:45 +08:00 committed by GitHub
parent 1df8fb3015
commit 7b4ffbcd3c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 2 deletions

View file

@ -626,8 +626,8 @@ func (d Decimal) DivRound(d2 Decimal, precision int32) Decimal {
// Mod returns d % d2. // Mod returns d % d2.
func (d Decimal) Mod(d2 Decimal) Decimal { func (d Decimal) Mod(d2 Decimal) Decimal {
quo := d.DivRound(d2, -d.exp+1).Truncate(0) _, r := d.QuoRem(d2, 0)
return d.Sub(d2.Mul(quo)) return r
} }
// Pow returns d to the power d2 // Pow returns d to the power d2

View file

@ -2147,6 +2147,8 @@ func TestDecimal_Mod(t *testing.T) {
Inp{"-7.5", "2"}: "-1.5", Inp{"-7.5", "2"}: "-1.5",
Inp{"7.5", "-2"}: "1.5", Inp{"7.5", "-2"}: "1.5",
Inp{"-7.5", "-2"}: "-1.5", Inp{"-7.5", "-2"}: "-1.5",
Inp{"41", "21"}: "20",
Inp{"400000000001", "200000000001"}: "200000000000",
} }
for inp, res := range inputs { for inp, res := range inputs {