Skip to content

Commit

Permalink
refactor(osmomath): replace Power with PowerInteger (backport #3712) (#…
Browse files Browse the repository at this point in the history
…3727)

* refactor(osmomath): replace Power with PowerInteger (backport #3712)

* Update CHANGELOG.md

Co-authored-by: Roman <[email protected]>
  • Loading branch information
mergify[bot] and p0mvn authored Dec 14, 2022
1 parent dc3de03 commit 567b9d1
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 43 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ 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`

## v13.0.0

Expand Down
20 changes: 1 addition & 19 deletions osmomath/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand All @@ -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) {
Expand Down
20 changes: 0 additions & 20 deletions osmomath/decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions osmoutils/binary_search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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},
Expand Down

0 comments on commit 567b9d1

Please sign in to comment.