Skip to content

Commit

Permalink
Fix mod when the divisor is slightly greater than the half of dividend (
Browse files Browse the repository at this point in the history
  • Loading branch information
zlasd committed Jan 10, 2024
1 parent 1df8fb3 commit 7b4ffbc
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
4 changes: 2 additions & 2 deletions decimal.go
Expand Up @@ -626,8 +626,8 @@ func (d Decimal) DivRound(d2 Decimal, precision int32) Decimal {

// Mod returns d % d2.
func (d Decimal) Mod(d2 Decimal) Decimal {
quo := d.DivRound(d2, -d.exp+1).Truncate(0)
return d.Sub(d2.Mul(quo))
_, r := d.QuoRem(d2, 0)
return r
}

// Pow returns d to the power d2
Expand Down
2 changes: 2 additions & 0 deletions decimal_test.go
Expand Up @@ -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{"41", "21"}: "20",
Inp{"400000000001", "200000000001"}: "200000000000",
}

for inp, res := range inputs {
Expand Down

0 comments on commit 7b4ffbc

Please sign in to comment.