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

osmomath: AddMut and QuoMut #3779

Merged
merged 13 commits into from
Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions 548207900.mdttt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
- mut add
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove this

- test add mut
- quo mut
- test quo mut/ remove want from test struct

<!-- < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < ☺
v ✰ Thanks for creating a PR! ✰
v Before smashing the submit button please review the checkboxes.
v If a checkbox is n/a - please still include it but + a little note why
v If your PR doesn't close an issue, that's OK! Just remove the Closes: #XXX line!
☺ > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > -->

Closes: #3773

## What is the purpose of the change
mutative add and quo + tests
25 changes: 25 additions & 0 deletions osmomath/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,17 @@ func (d BigDec) Add(d2 BigDec) BigDec {
return BigDec{res}
}

// mutative addition
func (d BigDec) AddMut(d2 BigDec) BigDec {
pysel marked this conversation as resolved.
Show resolved Hide resolved
d.i.Add(d.i, d2.i)

if d.i.BitLen() > maxDecBitLen {
panic("Int overflow")
}

return d
}

// subtraction
func (d BigDec) Sub(d2 BigDec) BigDec {
res := new(big.Int).Sub(d.i, d2.i)
Expand Down Expand Up @@ -332,6 +343,20 @@ func (d BigDec) Quo(d2 BigDec) BigDec {
return BigDec{chopped}
}

// mutative quotient
func (d BigDec) QuoMut(d2 BigDec) BigDec {
pysel marked this conversation as resolved.
Show resolved Hide resolved
// multiply precision twice
d.i.Mul(d.i, precisionReuse)
d.i.Mul(d.i, precisionReuse)

d.i.Quo(d.i, d2.i)
chopPrecisionAndRound(d.i)

if d.i.BitLen() > maxDecBitLen {
panic("Int overflow")
}
return d
}
func (d BigDec) QuoRaw(d2 int64) BigDec {
// multiply precision, so we can chop it later
mul := new(big.Int).Mul(d.i, precisionReuse)
Expand Down
53 changes: 53 additions & 0 deletions osmomath/decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,59 @@ func (s *decimalTestSuite) assertMutResult(expectedResult, startValue, mutativeR
s.Require().Equal(nonMutativeStartValue, startValue)
}

func TestAddMut(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since Add is supposed to call AddMut, can you please do a short test like this instead of duplicating test cases?

tests := []struct {
testing osmomath.BigDec
adding osmomath.BigDec
}{
{osmomath.NewBigDec(0), osmomath.NewBigDec(0)},
{osmomath.NewBigDec(1), osmomath.NewBigDec(1)},
{osmomath.NewBigDec(10), osmomath.NewBigDec(1)},
{osmomath.NewBigDec(12340), osmomath.NewBigDec(10)},
{osmomath.NewDecWithPrec(12340, 4), osmomath.NewDecWithPrec(12340, 4)},
{osmomath.NewDecWithPrec(12340, 5), osmomath.NewDecWithPrec(12, 5)},
{osmomath.NewDecWithPrec(12340, 8), osmomath.NewDecWithPrec(1009009009009009009, 8)},
{osmomath.NewDecWithPrec(1009009009009009009, 17), osmomath.NewDecWithPrec(100, 17)},
}
for _, tc := range tests {
want := tc.testing.Add(tc.adding)
tc.testing.AddMut(tc.adding)

require.Equal(t, want, tc.testing)
}
}

func TestQuoMut(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for this test case

tests := []struct {
testing osmomath.BigDec
quo osmomath.BigDec

expectingErr bool
}{
{osmomath.NewBigDec(0), osmomath.NewBigDec(0), true},
{osmomath.NewBigDec(1), osmomath.NewBigDec(1), false},
{osmomath.NewBigDec(10), osmomath.NewBigDec(1), false},
{osmomath.NewBigDec(12340), osmomath.NewBigDec(10), false},
{osmomath.NewDecWithPrec(12340, 4), osmomath.NewDecWithPrec(12340, 4), false},
{osmomath.NewDecWithPrec(12340, 5), osmomath.NewDecWithPrec(12, 5), false},
{osmomath.NewDecWithPrec(1009009009009009009, 8), osmomath.NewDecWithPrec(1000, 8), false},
{osmomath.NewDecWithPrec(500, 17), osmomath.NewDecWithPrec(100, 17), false},
}
for _, tc := range tests {
if !tc.expectingErr {
want := tc.testing.Quo(tc.quo)
tc.testing.QuoMut(tc.quo)

require.Equal(t, want, tc.testing)
} else {
save := tc.testing

require.Panics(t, func() { tc.testing.QuoMut(tc.quo) })
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a helper for this here:

func ConditionalPanic(t *testing.T, expectPanic bool, sut func()) {

require.Equal(t, save, tc.testing)
}

}
}
func TestDecApproxEq(t *testing.T) {
// d1 = 0.55, d2 = 0.6, tol = 0.1
d1 := osmomath.NewDecWithPrec(55, 2)
Expand Down
6 changes: 3 additions & 3 deletions osmomath/exp2.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ func exp2ChebyshevRationalApprox(x BigDec) BigDec {
for i := 1; i < len(numeratorCoefficients13Param); i++ {
x_exp_i.MulMut(x)

h_x = h_x.Add(numeratorCoefficients13Param[i].Mul(x_exp_i))
p_x = p_x.Add(denominatorCoefficients13Param[i].Mul(x_exp_i))
h_x.AddMut(numeratorCoefficients13Param[i].Mul(x_exp_i))
p_x.AddMut(denominatorCoefficients13Param[i].Mul(x_exp_i))
pysel marked this conversation as resolved.
Show resolved Hide resolved
}

return h_x.Quo(p_x)
return h_x.QuoMut(p_x)
}