diff --git a/decimal.go b/decimal.go index 6f7c41e..0cd28be 100644 --- a/decimal.go +++ b/decimal.go @@ -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 diff --git a/decimal_test.go b/decimal_test.go index 7dc5be6..17d4627 100644 --- a/decimal_test.go +++ b/decimal_test.go @@ -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 {