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 mod when the divisor is slightly greater than the half of dividend (#312) #317

Merged
merged 1 commit into from Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions decimal.go
Expand Up @@ -628,8 +628,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 @@ -2143,6 +2143,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