From 8e52770ed355b30ce1d91e1e717eb0e9d2a4e829 Mon Sep 17 00:00:00 2001 From: Roman Date: Wed, 14 Dec 2022 11:52:17 -0800 Subject: [PATCH] refactor(osmomath): replace Power with PowerInteger (backport #3712) --- CHANGELOG.md | 2 ++ osmomath/decimal.go | 20 +------------------- osmomath/decimal_test.go | 20 -------------------- osmoutils/binary_search_test.go | 8 ++++---- 4 files changed, 7 insertions(+), 43 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d0b09d3ea4..ced8438ea95 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -75,6 +75,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * [#3611](https://github.com/osmosis-labs/osmosis/pull/3611),[#3647](https://github.com/osmosis-labs/osmosis/pull/3647) Introduce osmocli, to automate thousands of lines of CLI boilerplate * [#3634](https://github.com/osmosis-labs/osmosis/pull/3634) (Makefile) Ensure correct golang version in make build and make install. (Thank you @jhernandezb ) +* [#3712](https://github.com/osmosis-labs/osmosis/pull/3712) replace `osmomath.BigDec` `Power` with `PowerInteger` +* [#3711](https://github.com/osmosis-labs/osmosis/pull/3711) Use Dec instead of Int for additive `ErrTolerace` in `osmoutils`. ## v13.0.0 diff --git a/osmomath/decimal.go b/osmomath/decimal.go index 1847c244ba0..955b6229f58 100644 --- a/osmomath/decimal.go +++ b/osmomath/decimal.go @@ -421,7 +421,7 @@ func (d BigDec) ApproxRoot(root uint64) (guess BigDec, err error) { guess, delta := OneDec(), OneDec() for iter := 0; delta.Abs().GT(SmallestDec()) && iter < maxApproxRootIterations; iter++ { - prev := guess.Power(root - 1) + prev := guess.PowerInteger(root - 1) if prev.IsZero() { prev = SmallestDec() } @@ -435,24 +435,6 @@ func (d BigDec) ApproxRoot(root uint64) (guess BigDec, err error) { return guess, nil } -// Power returns a the result of raising to a positive integer power -func (d BigDec) Power(power uint64) BigDec { - if power == 0 { - return OneDec() - } - tmp := OneDec() - - for i := power; i > 1; { - if i%2 != 0 { - tmp = tmp.Mul(d) - } - i /= 2 - d = d.Mul(d) - } - - return d.Mul(tmp) -} - // ApproxSqrt is a wrapper around ApproxRoot for the common special case // of finding the square root of a number. It returns -(sqrt(abs(d)) if input is negative. func (d BigDec) ApproxSqrt() (BigDec, error) { diff --git a/osmomath/decimal_test.go b/osmomath/decimal_test.go index fff7d7fa8fd..c6983617c6c 100644 --- a/osmomath/decimal_test.go +++ b/osmomath/decimal_test.go @@ -475,26 +475,6 @@ func (s *decimalTestSuite) TestDecCeil() { } } -func (s *decimalTestSuite) TestPower() { - testCases := []struct { - input osmomath.BigDec - power uint64 - expected osmomath.BigDec - }{ - {osmomath.OneDec(), 10, osmomath.OneDec()}, // 1.0 ^ (10) => 1.0 - {osmomath.NewDecWithPrec(5, 1), 2, osmomath.NewDecWithPrec(25, 2)}, // 0.5 ^ 2 => 0.25 - {osmomath.NewDecWithPrec(2, 1), 2, osmomath.NewDecWithPrec(4, 2)}, // 0.2 ^ 2 => 0.04 - {osmomath.NewDecFromInt(osmomath.NewInt(3)), 3, osmomath.NewDecFromInt(osmomath.NewInt(27))}, // 3 ^ 3 => 27 - {osmomath.NewDecFromInt(osmomath.NewInt(-3)), 4, osmomath.NewDecFromInt(osmomath.NewInt(81))}, // -3 ^ 4 = 81 - {osmomath.MustNewDecFromStr("1.414213562373095048801688724209698079"), 2, osmomath.NewDecFromInt(osmomath.NewInt(2))}, // 1.414213562373095048801688724209698079 ^ 2 = 2 - } - - for i, tc := range testCases { - res := tc.input.Power(tc.power) - s.Require().True(tc.expected.Sub(res).Abs().LTE(osmomath.SmallestDec()), "unexpected result for test case %d, input: %v", i, tc.input) - } -} - func (s *decimalTestSuite) TestApproxRoot() { testCases := []struct { input osmomath.BigDec diff --git a/osmoutils/binary_search_test.go b/osmoutils/binary_search_test.go index 95f7d5afb56..5ce7dc027e6 100644 --- a/osmoutils/binary_search_test.go +++ b/osmoutils/binary_search_test.go @@ -82,13 +82,13 @@ func lineF(a osmomath.BigDec) (osmomath.BigDec, error) { return a, nil } func cubicF(a osmomath.BigDec) (osmomath.BigDec, error) { - return a.Power(3), nil + return a.PowerInteger(3), nil } -var negCubicFConstant = osmomath.NewBigDec(1 << 62).Power(3).Neg() +var negCubicFConstant = osmomath.NewBigDec(1 << 62).PowerInteger(3).Neg() func negCubicF(a osmomath.BigDec) (osmomath.BigDec, error) { - return a.Power(3).Add(negCubicFConstant), nil + return a.PowerInteger(3).Add(negCubicFConstant), nil } type searchFn func(osmomath.BigDec) (osmomath.BigDec, error) @@ -232,7 +232,7 @@ func TestBinarySearchBigDec(t *testing.T) { twoTo50 := osmomath.NewBigDec(1 << 50) twoTo25PlusOne := osmomath.NewBigDec(1 + (1 << 25)) - twoTo25PlusOneCubed := twoTo25PlusOne.Power(3) + twoTo25PlusOneCubed := twoTo25PlusOne.PowerInteger(3) tests := map[string]binarySearchTestCase{ "cubic f, no err tolerance, converges": {cubicF, zero, twoTo50, twoTo25PlusOneCubed, withinOne, 51, twoTo25PlusOne, false},