Skip to content

Commit

Permalink
Merge branch 'math' into main
Browse files Browse the repository at this point in the history
# Conflicts:
#	x/gamm/keeper/pool/math/math.go
  • Loading branch information
hyehan committed Oct 18, 2020
2 parents e27a74b + 1600d22 commit b616d46
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 7 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/rakyll/statik v0.1.7
github.com/spf13/cast v1.3.1
github.com/spf13/cobra v1.0.0
github.com/stretchr/testify v1.6.1
github.com/tendermint/tendermint v0.34.0-rc3.0.20200907055413-3359e0bf2f84
github.com/tendermint/tm-db v0.6.2
)
Expand Down
24 changes: 17 additions & 7 deletions x/gamm/keeper/pool/math.go → x/gamm/keeper/pool/math/math.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package pool
package math

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand Down Expand Up @@ -177,10 +179,12 @@ func subSign(a, b sdk.Dec) (sdk.Dec, bool) {
}

func pow(base sdk.Dec, exp sdk.Dec) sdk.Dec {

// TODO: MIN_BPOW_BASE, MAX_BPOW_BASE identification required
// require(base >= MIN_BPOW_BASE, "ERR_BPOW_BASE_TOO_LOW");
// require(base <= MAX_BPOW_BASE, "ERR_BPOW_BASE_TOO_HIGH");
if base.LTE(sdk.ZeroDec()) {
panic(fmt.Errorf("base have to be greater than zero"))
}
if base.GTE(sdk.OneDec().MulInt64(2)) {
panic(fmt.Errorf("base have to be lesser than two"))
}

whole := exp.TruncateDec()
remain := exp.Sub(whole)
Expand All @@ -197,10 +201,17 @@ func pow(base sdk.Dec, exp sdk.Dec) sdk.Dec {
}

func powApprox(base sdk.Dec, exp sdk.Dec, precision sdk.Dec) sdk.Dec {
if base.LTE(sdk.ZeroDec()) {
panic(fmt.Errorf("base have to be greater than zero"))
}
if base.GTE(sdk.OneDec().MulInt64(2)) {
panic(fmt.Errorf("base have to be lesser than two"))
}

a := exp
x, xneg := subSign(base, sdk.OneDec())
term := sdk.OneDec()
sum := term
sum := sdk.OneDec()
negative := false

for i := 1; term.GTE(precision); i++ {
Expand All @@ -212,7 +223,6 @@ func powApprox(base sdk.Dec, exp sdk.Dec, precision sdk.Dec) sdk.Dec {
if term.IsZero() {
break
}

if xneg {
negative = !negative
}
Expand Down
45 changes: 45 additions & 0 deletions x/gamm/keeper/pool/math/math_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package math

import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/stretchr/testify/require"
)

func TestPowApprox(t *testing.T) {
base, err := sdk.NewDecFromStr("1.5")
require.NoError(t, err)
exp, err := sdk.NewDecFromStr("0.4")
require.NoError(t, err)
precision, err := sdk.NewDecFromStr("0.00000001")
require.NoError(t, err)

s := powApprox(base, exp, precision)
expectedDec, err := sdk.NewDecFromStr("1.17607902")
require.NoError(t, err)

require.True(
t,
expectedDec.Sub(s).Abs().LTE(precision),
"expected value & actual value's difference should less than precision",
)
}

func TestPow(t *testing.T) {
base, err := sdk.NewDecFromStr("1.4")
require.NoError(t, err)
exp, err := sdk.NewDecFromStr("3.2")
require.NoError(t, err)

s := pow(base, exp)
expectedDec, err := sdk.NewDecFromStr("2.93501087")
require.NoError(t, err)

require.True(
t,
expectedDec.Sub(s).Abs().LTE(powPrecision),
"expected value & actual value's difference should less than precision",
)
}

0 comments on commit b616d46

Please sign in to comment.