diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d0b09d3ea4..f52e85b00e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -75,6 +75,11 @@ 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 ) +<<<<<<< HEAD +======= +* [#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`. +>>>>>>> 5df8cd03 (refactor(osmomath): replace Power with PowerInteger (#3712)) ## 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},