From 28ac3a7a65ad0380acb51574dadb2f811c04fc99 Mon Sep 17 00:00:00 2001 From: Ruslan Akhtariev Date: Sat, 17 Dec 2022 10:56:17 +0100 Subject: [PATCH 01/13] mut add --- osmomath/decimal.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/osmomath/decimal.go b/osmomath/decimal.go index 955b6229f58..72546a1386e 100644 --- a/osmomath/decimal.go +++ b/osmomath/decimal.go @@ -248,6 +248,16 @@ func (d BigDec) Add(d2 BigDec) BigDec { return BigDec{res} } +// mutative addition +func (d *BigDec) AddMut(d2 BigDec) { + res := new(big.Int).Add(d.i, d2.i) + + if res.BitLen() > maxDecBitLen { + panic("Int overflow") + } + d.i = res +} + // subtraction func (d BigDec) Sub(d2 BigDec) BigDec { res := new(big.Int).Sub(d.i, d2.i) From e0e260175ca90e137ef23a473689d606c8f3acfc Mon Sep 17 00:00:00 2001 From: Ruslan Akhtariev Date: Sat, 17 Dec 2022 11:06:55 +0100 Subject: [PATCH 02/13] test add mut --- osmomath/decimal_test.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/osmomath/decimal_test.go b/osmomath/decimal_test.go index a33e0864733..9e3d0371893 100644 --- a/osmomath/decimal_test.go +++ b/osmomath/decimal_test.go @@ -38,6 +38,26 @@ func (s *decimalTestSuite) assertMutResult(expectedResult, startValue, mutativeR s.Require().Equal(nonMutativeStartValue, startValue) } +func TestAddMut(t *testing.T) { + tests := []struct { + testing osmomath.BigDec + adding osmomath.BigDec + want osmomath.BigDec + }{ + {osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0)}, + {osmomath.NewBigDec(1), osmomath.NewBigDec(1), osmomath.NewBigDec(2)}, + {osmomath.NewBigDec(10), osmomath.NewBigDec(1), osmomath.NewBigDec(11)}, + {osmomath.NewBigDec(12340), osmomath.NewBigDec(10), osmomath.NewBigDec(12350)}, + {osmomath.NewDecWithPrec(12340, 4), osmomath.NewDecWithPrec(12340, 4), osmomath.NewDecWithPrec(24680, 4)}, + {osmomath.NewDecWithPrec(12340, 5), osmomath.NewDecWithPrec(12, 5), osmomath.NewDecWithPrec(12352, 5)}, + {osmomath.NewDecWithPrec(12340, 8), osmomath.NewDecWithPrec(1009009009009009009, 8), osmomath.NewDecWithPrec(1009009009009021349, 8)}, + {osmomath.NewDecWithPrec(1009009009009009009, 17), osmomath.NewDecWithPrec(100, 17), osmomath.NewDecWithPrec(1009009009009009109, 17)}, + } + for _, tc := range tests { + tc.testing.AddMut(tc.adding) + require.Equal(t, tc.want, tc.testing) + } +} func TestDecApproxEq(t *testing.T) { // d1 = 0.55, d2 = 0.6, tol = 0.1 d1 := osmomath.NewDecWithPrec(55, 2) From 4978cbee914e8baacdcedc9fc3fa81d39439d7d8 Mon Sep 17 00:00:00 2001 From: Ruslan Akhtariev Date: Sat, 17 Dec 2022 11:07:35 +0100 Subject: [PATCH 03/13] quo mut --- osmomath/decimal.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/osmomath/decimal.go b/osmomath/decimal.go index 72546a1386e..7d2b3785858 100644 --- a/osmomath/decimal.go +++ b/osmomath/decimal.go @@ -342,6 +342,20 @@ func (d BigDec) Quo(d2 BigDec) BigDec { return BigDec{chopped} } +// mutative quotient +func (d *BigDec) QuoMut(d2 BigDec) { + // multiply precision twice + mul := new(big.Int).Mul(d.i, precisionReuse) + mul.Mul(mul, precisionReuse) + + quo := new(big.Int).Quo(mul, d2.i) + chopped := chopPrecisionAndRound(quo) + + if chopped.BitLen() > maxDecBitLen { + panic("Int overflow") + } + d.i = chopped +} func (d BigDec) QuoRaw(d2 int64) BigDec { // multiply precision, so we can chop it later mul := new(big.Int).Mul(d.i, precisionReuse) From 28701147cddaa9dbab36f3c451205459355b2a5b Mon Sep 17 00:00:00 2001 From: Ruslan Akhtariev Date: Sat, 17 Dec 2022 11:29:25 +0100 Subject: [PATCH 04/13] test quo mut/ remove want from test struct --- osmomath/decimal_test.go | 53 ++++++++++++++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 10 deletions(-) diff --git a/osmomath/decimal_test.go b/osmomath/decimal_test.go index 9e3d0371893..f6e0f30faea 100644 --- a/osmomath/decimal_test.go +++ b/osmomath/decimal_test.go @@ -42,20 +42,53 @@ func TestAddMut(t *testing.T) { tests := []struct { testing osmomath.BigDec adding osmomath.BigDec - want osmomath.BigDec }{ - {osmomath.NewBigDec(0), osmomath.NewBigDec(0), osmomath.NewBigDec(0)}, - {osmomath.NewBigDec(1), osmomath.NewBigDec(1), osmomath.NewBigDec(2)}, - {osmomath.NewBigDec(10), osmomath.NewBigDec(1), osmomath.NewBigDec(11)}, - {osmomath.NewBigDec(12340), osmomath.NewBigDec(10), osmomath.NewBigDec(12350)}, - {osmomath.NewDecWithPrec(12340, 4), osmomath.NewDecWithPrec(12340, 4), osmomath.NewDecWithPrec(24680, 4)}, - {osmomath.NewDecWithPrec(12340, 5), osmomath.NewDecWithPrec(12, 5), osmomath.NewDecWithPrec(12352, 5)}, - {osmomath.NewDecWithPrec(12340, 8), osmomath.NewDecWithPrec(1009009009009009009, 8), osmomath.NewDecWithPrec(1009009009009021349, 8)}, - {osmomath.NewDecWithPrec(1009009009009009009, 17), osmomath.NewDecWithPrec(100, 17), osmomath.NewDecWithPrec(1009009009009009109, 17)}, + {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, tc.want, tc.testing) + + require.Equal(t, want, tc.testing) + } +} + +func TestQuoMut(t *testing.T) { + 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) }) + require.Equal(t, save, tc.testing) + } + } } func TestDecApproxEq(t *testing.T) { From 2faea41623c90cc0ff92fd017bfc11508afc399a Mon Sep 17 00:00:00 2001 From: Ruslan Akhtariev Date: Sat, 17 Dec 2022 11:34:27 +0100 Subject: [PATCH 05/13] refactor exp --- osmomath/exp2.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/osmomath/exp2.go b/osmomath/exp2.go index b7973b3bc62..a605dde99cc 100644 --- a/osmomath/exp2.go +++ b/osmomath/exp2.go @@ -93,8 +93,8 @@ 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)) } return h_x.Quo(p_x) From 099327258f017f5ad7d78c9a7738ed7235c9a8b5 Mon Sep 17 00:00:00 2001 From: Ruslan Akhtariev Date: Sat, 17 Dec 2022 19:48:22 +0100 Subject: [PATCH 06/13] change mutatives code --- osmomath/decimal.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/osmomath/decimal.go b/osmomath/decimal.go index 7d2b3785858..a4f43899e00 100644 --- a/osmomath/decimal.go +++ b/osmomath/decimal.go @@ -250,12 +250,11 @@ func (d BigDec) Add(d2 BigDec) BigDec { // mutative addition func (d *BigDec) AddMut(d2 BigDec) { - res := new(big.Int).Add(d.i, d2.i) + d.i = new(big.Int).Add(d.i, d2.i) - if res.BitLen() > maxDecBitLen { + if d.i.BitLen() > maxDecBitLen { panic("Int overflow") } - d.i = res } // subtraction @@ -349,12 +348,11 @@ func (d *BigDec) QuoMut(d2 BigDec) { mul.Mul(mul, precisionReuse) quo := new(big.Int).Quo(mul, d2.i) - chopped := chopPrecisionAndRound(quo) + d.i = chopPrecisionAndRound(quo) - if chopped.BitLen() > maxDecBitLen { + if d.i.BitLen() > maxDecBitLen { panic("Int overflow") } - d.i = chopped } func (d BigDec) QuoRaw(d2 int64) BigDec { // multiply precision, so we can chop it later From 5fec9f4f7522e109572b1472f561642cce242907 Mon Sep 17 00:00:00 2001 From: Ruslan Akhtariev Date: Sat, 17 Dec 2022 19:59:57 +0100 Subject: [PATCH 07/13] change --- osmomath/decimal.go | 13 ++++++++----- osmomath/decimal_test.go | 1 + 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/osmomath/decimal.go b/osmomath/decimal.go index a4f43899e00..ddbd3241e6e 100644 --- a/osmomath/decimal.go +++ b/osmomath/decimal.go @@ -249,12 +249,14 @@ func (d BigDec) Add(d2 BigDec) BigDec { } // mutative addition -func (d *BigDec) AddMut(d2 BigDec) { - d.i = new(big.Int).Add(d.i, d2.i) +func (d BigDec) AddMut(d2 BigDec) BigDec { + d.i.Add(d.i, d2.i) if d.i.BitLen() > maxDecBitLen { panic("Int overflow") } + + return d } // subtraction @@ -342,17 +344,18 @@ func (d BigDec) Quo(d2 BigDec) BigDec { } // mutative quotient -func (d *BigDec) QuoMut(d2 BigDec) { +func (d BigDec) QuoMut(d2 BigDec) BigDec { // multiply precision twice mul := new(big.Int).Mul(d.i, precisionReuse) mul.Mul(mul, precisionReuse) - quo := new(big.Int).Quo(mul, d2.i) - d.i = chopPrecisionAndRound(quo) + d.i.Quo(mul, d2.i) + d.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 diff --git a/osmomath/decimal_test.go b/osmomath/decimal_test.go index f6e0f30faea..c8d7d7a7655 100644 --- a/osmomath/decimal_test.go +++ b/osmomath/decimal_test.go @@ -79,6 +79,7 @@ func TestQuoMut(t *testing.T) { for _, tc := range tests { if !tc.expectingErr { want := tc.testing.Quo(tc.quo) + fmt.Println(tc.testing, tc.quo) tc.testing.QuoMut(tc.quo) require.Equal(t, want, tc.testing) From 0a20620708285751b04f7e6546431c70d330e94d Mon Sep 17 00:00:00 2001 From: Ruslan Akhtariev Date: Sun, 18 Dec 2022 09:04:03 +0100 Subject: [PATCH 08/13] not allocaing --- 548207900.mdttt | 16 ++++++++++++++++ osmomath/decimal.go | 8 ++++---- osmomath/decimal_test.go | 1 - 3 files changed, 20 insertions(+), 5 deletions(-) create mode 100644 548207900.mdttt diff --git a/548207900.mdttt b/548207900.mdttt new file mode 100644 index 00000000000..bb7d0b6b694 --- /dev/null +++ b/548207900.mdttt @@ -0,0 +1,16 @@ +- mut add +- test add mut +- quo mut +- test quo mut/ remove want from test struct + + + +Closes: #3773 + +## What is the purpose of the change +mutative add and quo + tests diff --git a/osmomath/decimal.go b/osmomath/decimal.go index ddbd3241e6e..fed6ef3acfc 100644 --- a/osmomath/decimal.go +++ b/osmomath/decimal.go @@ -346,11 +346,11 @@ func (d BigDec) Quo(d2 BigDec) BigDec { // mutative quotient func (d BigDec) QuoMut(d2 BigDec) BigDec { // multiply precision twice - mul := new(big.Int).Mul(d.i, precisionReuse) - mul.Mul(mul, precisionReuse) + d.i.Mul(d.i, precisionReuse) + d.i.Mul(d.i, precisionReuse) - d.i.Quo(mul, d2.i) - d.i = chopPrecisionAndRound(d.i) + d.i.Quo(d.i, d2.i) + chopPrecisionAndRound(d.i) if d.i.BitLen() > maxDecBitLen { panic("Int overflow") diff --git a/osmomath/decimal_test.go b/osmomath/decimal_test.go index c8d7d7a7655..f6e0f30faea 100644 --- a/osmomath/decimal_test.go +++ b/osmomath/decimal_test.go @@ -79,7 +79,6 @@ func TestQuoMut(t *testing.T) { for _, tc := range tests { if !tc.expectingErr { want := tc.testing.Quo(tc.quo) - fmt.Println(tc.testing, tc.quo) tc.testing.QuoMut(tc.quo) require.Equal(t, want, tc.testing) From 0b22fd08947f5cf61c6abffa62b0543f3bc96f30 Mon Sep 17 00:00:00 2001 From: Ruslan Akhtariev Date: Sun, 18 Dec 2022 09:05:35 +0100 Subject: [PATCH 09/13] exp change to quomut --- osmomath/exp2.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/osmomath/exp2.go b/osmomath/exp2.go index a605dde99cc..39b2333ebd3 100644 --- a/osmomath/exp2.go +++ b/osmomath/exp2.go @@ -97,5 +97,5 @@ func exp2ChebyshevRationalApprox(x BigDec) BigDec { p_x.AddMut(denominatorCoefficients13Param[i].Mul(x_exp_i)) } - return h_x.Quo(p_x) + return h_x.QuoMut(p_x) } From 8be5795b1bb359ee4493a3b790c58b3713e69483 Mon Sep 17 00:00:00 2001 From: Ruslan Akhtariev Date: Tue, 20 Dec 2022 11:59:50 +0100 Subject: [PATCH 10/13] remove file --- 548207900.mdttt | 16 ---------------- 1 file changed, 16 deletions(-) delete mode 100644 548207900.mdttt diff --git a/548207900.mdttt b/548207900.mdttt deleted file mode 100644 index bb7d0b6b694..00000000000 --- a/548207900.mdttt +++ /dev/null @@ -1,16 +0,0 @@ -- mut add -- test add mut -- quo mut -- test quo mut/ remove want from test struct - - - -Closes: #3773 - -## What is the purpose of the change -mutative add and quo + tests From f28009950d1149a0089f8c3355072741b4c4799f Mon Sep 17 00:00:00 2001 From: Ruslan Akhtariev Date: Tue, 20 Dec 2022 12:02:13 +0100 Subject: [PATCH 11/13] refactor quo --- osmomath/decimal.go | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/osmomath/decimal.go b/osmomath/decimal.go index fed6ef3acfc..4a0812482ee 100644 --- a/osmomath/decimal.go +++ b/osmomath/decimal.go @@ -330,17 +330,9 @@ func (d BigDec) MulInt64(i int64) BigDec { // quotient func (d BigDec) Quo(d2 BigDec) BigDec { - // multiply precision twice - mul := new(big.Int).Mul(d.i, precisionReuse) - mul.Mul(mul, precisionReuse) - - quo := new(big.Int).Quo(mul, d2.i) - chopped := chopPrecisionAndRound(quo) - - if chopped.BitLen() > maxDecBitLen { - panic("Int overflow") - } - return BigDec{chopped} + copy := d.Clone() + copy.QuoMut(d2) + return copy } // mutative quotient From 8b11ef6603384c3f024be259067a22ff9fb221cd Mon Sep 17 00:00:00 2001 From: Ruslan Akhtariev Date: Tue, 20 Dec 2022 12:02:48 +0100 Subject: [PATCH 12/13] refactor ad --- osmomath/decimal.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/osmomath/decimal.go b/osmomath/decimal.go index 4a0812482ee..e7801b481b5 100644 --- a/osmomath/decimal.go +++ b/osmomath/decimal.go @@ -240,12 +240,9 @@ func (d BigDec) BigInt() *big.Int { // addition func (d BigDec) Add(d2 BigDec) BigDec { - res := new(big.Int).Add(d.i, d2.i) - - if res.BitLen() > maxDecBitLen { - panic("Int overflow") - } - return BigDec{res} + copy := d.Clone() + copy.AddMut(d2) + return copy } // mutative addition From 336e7b6f8601b56d5b81c9ee1df2c1899872929d Mon Sep 17 00:00:00 2001 From: Ruslan Akhtariev Date: Tue, 20 Dec 2022 12:20:52 +0100 Subject: [PATCH 13/13] refactor tests --- osmomath/decimal_test.go | 74 ++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 40 deletions(-) diff --git a/osmomath/decimal_test.go b/osmomath/decimal_test.go index f6e0f30faea..8ca1c58657c 100644 --- a/osmomath/decimal_test.go +++ b/osmomath/decimal_test.go @@ -38,57 +38,51 @@ func (s *decimalTestSuite) assertMutResult(expectedResult, startValue, mutativeR s.Require().Equal(nonMutativeStartValue, startValue) } -func TestAddMut(t *testing.T) { - tests := []struct { - testing osmomath.BigDec - adding osmomath.BigDec +func (s *decimalTestSuite) TestAddMut() { + toAdd := osmomath.MustNewDecFromStr("10") + tests := map[string]struct { + startValue osmomath.BigDec + expectedMutResult 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)}, + "0": {osmomath.NewBigDec(0), osmomath.NewBigDec(10)}, + "1": {osmomath.NewBigDec(1), osmomath.NewBigDec(11)}, + "10": {osmomath.NewBigDec(10), osmomath.NewBigDec(20)}, } - for _, tc := range tests { - want := tc.testing.Add(tc.adding) - tc.testing.AddMut(tc.adding) - require.Equal(t, want, tc.testing) + for name, tc := range tests { + s.Run(name, func() { + startMut := tc.startValue.Clone() + startNonMut := tc.startValue.Clone() + + resultMut := startMut.AddMut(toAdd) + resultNonMut := startNonMut.Add(toAdd) + + s.assertMutResult(tc.expectedMutResult, tc.startValue, resultMut, resultNonMut, startMut, startNonMut) + }) } } -func TestQuoMut(t *testing.T) { - tests := []struct { - testing osmomath.BigDec - quo osmomath.BigDec - - expectingErr bool +func (s *decimalTestSuite) TestQuoMut() { + quoBy := osmomath.MustNewDecFromStr("2") + tests := map[string]struct { + startValue osmomath.BigDec + expectedMutResult osmomath.BigDec }{ - {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}, + "0": {osmomath.NewBigDec(0), osmomath.NewBigDec(0)}, + "1": {osmomath.NewBigDec(1), osmomath.MustNewDecFromStr("0.5")}, + "10": {osmomath.NewBigDec(10), osmomath.NewBigDec(5)}, } - 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 + for name, tc := range tests { + s.Run(name, func() { + startMut := tc.startValue.Clone() + startNonMut := tc.startValue.Clone() - require.Panics(t, func() { tc.testing.QuoMut(tc.quo) }) - require.Equal(t, save, tc.testing) - } + resultMut := startMut.QuoMut(quoBy) + resultNonMut := startNonMut.Quo(quoBy) + s.assertMutResult(tc.expectedMutResult, tc.startValue, resultMut, resultNonMut, startMut, startNonMut) + }) } } func TestDecApproxEq(t *testing.T) {