Skip to content

Commit

Permalink
address some comments
Browse files Browse the repository at this point in the history
  • Loading branch information
tac0turtle committed May 28, 2024
1 parent c4b9ccd commit 809884a
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 3 deletions.
7 changes: 4 additions & 3 deletions math/dec.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,9 @@ func (d LegacyDec) QuoTruncate(d2 LegacyDec) LegacyDec {
return d.ImmutOp(LegacyDec.QuoTruncateMut, d2)
}

// QuoTruncateMut mutable quotient truncate
// QuoTruncateMut divides the current LegacyDec value by the provided LegacyDec value, truncating the result.
func (d LegacyDec) QuoTruncateMut(d2 LegacyDec) LegacyDec {
// multiply precision once
// multiply precision once before performing division
d.i.Mul(d.i, precisionReuse)
d.i.Quo(d.i, d2.i)

Expand All @@ -419,7 +419,8 @@ func (d LegacyDec) QuoRoundupMut(d2 LegacyDec) LegacyDec {
// multiply precision twice
d.i.Mul(d.i, precisionReuse)
_, rem := d.i.QuoRem(d.i, d2.i, big.NewInt(0))
if rem.Sign() != 0 {
if rem.Sign() > 0 && d.IsNegative() == d2.IsNegative() ||
rem.Sign() < 0 && d.IsNegative() != d2.IsNegative() {
d.i.Add(d.i, oneInt)
}

Expand Down
149 changes: 149 additions & 0 deletions math/dec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -790,3 +790,152 @@ func (s *decimalTestSuite) TestConvertToBigIntMutativeForLegacyDec() {
s.Require().NotEqual(big.NewInt(50), i.BigIntMut())
s.Require().NotEqual(big.NewInt(50), i.BigInt())
}

func TestQuoRoundupMut(t *testing.T) {
specs := map[string]struct {
dividend, divisor math.LegacyDec
exp string
}{
"0.0000000000000000001": {
dividend: math.LegacyNewDecWithPrec(1, 18),
divisor: math.LegacyMustNewDecFromStr("10"),
exp: "0.000000000000000001",
},
"0.0000000000000000002": {
dividend: math.LegacyNewDecWithPrec(1, 18),
divisor: math.LegacyMustNewDecFromStr("5"),
exp: "0.000000000000000001",
},
"0.0000000000000000003": {
dividend: math.LegacyNewDecWithPrec(1, 18),
divisor: math.LegacyMustNewDecFromStr("3.333333333333333"),
exp: "0.000000000000000001",
},
"0.0000000000000000004": {
dividend: math.LegacyNewDecWithPrec(1, 18),
divisor: math.LegacyMustNewDecFromStr("2.5"),
exp: "0.000000000000000001",
},
"0.0000000000000000005": {
dividend: math.LegacyNewDecWithPrec(1, 18),
divisor: math.LegacyMustNewDecFromStr("2"),
exp: "0.000000000000000001",
},
"0.0000000000000000006": {
dividend: math.LegacyNewDecWithPrec(1, 18),
divisor: math.LegacyMustNewDecFromStr("1.666666666666666666"),
exp: "0.000000000000000001",
},
"0.0000000000000000007": {
dividend: math.LegacyNewDecWithPrec(1, 18),
divisor: math.LegacyMustNewDecFromStr("1.428571428571429"),
exp: "0.000000000000000001",
},
"0.0000000000000000008": {
dividend: math.LegacyNewDecWithPrec(1, 18),
divisor: math.LegacyMustNewDecFromStr("1.25"),
exp: "0.000000000000000001",
},
"0.0000000000000000009": {
dividend: math.LegacyNewDecWithPrec(1, 18),
divisor: math.LegacyMustNewDecFromStr("1.111111111111111"),
exp: "0.000000000000000001",
},
"-0.0000000000000000001": {
dividend: math.LegacyNewDecWithPrec(1, 18).Neg(),
divisor: math.LegacyMustNewDecFromStr("10"),
exp: "0.000000000000000000",
},
"-0.0000000000000000002": {
dividend: math.LegacyNewDecWithPrec(1, 18).Neg(),
divisor: math.LegacyMustNewDecFromStr("5"),
exp: "0.000000000000000000",
},
"-0.0000000000000000003": {
dividend: math.LegacyNewDecWithPrec(1, 18).Neg(),
divisor: math.LegacyMustNewDecFromStr("3.333333333333333"),
exp: "0.000000000000000000",
},
"-0.0000000000000000004": {
dividend: math.LegacyNewDecWithPrec(1, 18).Neg(),
divisor: math.LegacyMustNewDecFromStr("2.5"),
exp: "0.000000000000000000",
},
"-0.0000000000000000005": {
dividend: math.LegacyNewDecWithPrec(1, 18).Neg(),
divisor: math.LegacyMustNewDecFromStr("2"),
exp: "0.000000000000000000",
},
"-0.0000000000000000006": {
dividend: math.LegacyNewDecWithPrec(1, 18).Neg(),
divisor: math.LegacyMustNewDecFromStr("1.666666666666666666"),
exp: "0.000000000000000000",
},
"-0.0000000000000000007": {
dividend: math.LegacyNewDecWithPrec(1, 18).Neg(),
divisor: math.LegacyMustNewDecFromStr("1.428571428571429"),
exp: "0.000000000000000000",
},
"-0.0000000000000000008": {
dividend: math.LegacyNewDecWithPrec(1, 18).Neg(),
divisor: math.LegacyMustNewDecFromStr("1.25"),
exp: "0.000000000000000000",
},
"-0.0000000000000000009": {
dividend: math.LegacyNewDecWithPrec(1, 18).Neg(),
divisor: math.LegacyMustNewDecFromStr("1.111111111111111"),
exp: "0.000000000000000000",
},
"--0.0000000000000000001": {
dividend: math.LegacyNewDecWithPrec(1, 18).Neg(),
divisor: math.LegacyMustNewDecFromStr("-10"),
exp: "0.000000000000000001",
},
"--0.0000000000000000002": {
dividend: math.LegacyNewDecWithPrec(1, 18).Neg(),
divisor: math.LegacyMustNewDecFromStr("-5"),
exp: "0.000000000000000001",
},
"--0.0000000000000000003": {
dividend: math.LegacyNewDecWithPrec(1, 18).Neg(),
divisor: math.LegacyMustNewDecFromStr("-3.333333333333333"),
exp: "0.000000000000000001",
},
"--0.0000000000000000004": {
dividend: math.LegacyNewDecWithPrec(1, 18).Neg(),
divisor: math.LegacyMustNewDecFromStr("-2.5"),
exp: "0.000000000000000001",
},
"--0.0000000000000000005": {
dividend: math.LegacyNewDecWithPrec(1, 18).Neg(),
divisor: math.LegacyMustNewDecFromStr("-2"),
exp: "0.000000000000000001",
},
"--0.0000000000000000006": {
dividend: math.LegacyNewDecWithPrec(1, 18).Neg(),
divisor: math.LegacyMustNewDecFromStr("-1.666666666666666666"),
exp: "0.000000000000000001",
},
"--0.0000000000000000007": {
dividend: math.LegacyNewDecWithPrec(1, 18).Neg(),
divisor: math.LegacyMustNewDecFromStr("-1.428571428571429"),
exp: "0.000000000000000001",
},
"--0.0000000000000000008": {
dividend: math.LegacyNewDecWithPrec(1, 18).Neg(),
divisor: math.LegacyMustNewDecFromStr("-1.25"),
exp: "0.000000000000000001",
},
"--0.0000000000000000009": {
dividend: math.LegacyNewDecWithPrec(1, 18).Neg(),
divisor: math.LegacyMustNewDecFromStr("-1.111111111111111"),
exp: "0.000000000000000001",
},
}
for name, spec := range specs {
t.Run(name, func(t *testing.T) {
got := spec.dividend.Clone().QuoRoundupMut(spec.divisor.Clone())
require.Equal(t, spec.exp, got.String())
})
}
}

0 comments on commit 809884a

Please sign in to comment.