Skip to content

Commit

Permalink
osmomath things
Browse files Browse the repository at this point in the history
  • Loading branch information
p0mvn committed Dec 5, 2022
1 parent fcf8133 commit 90f137e
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
19 changes: 19 additions & 0 deletions osmomath/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -972,3 +972,22 @@ func (x BigDec) CustomBaseLog(base BigDec) BigDec {

return y
}

// TODO: spec and tests - taken from sdk.
func (d BigDec) PowerMut(power uint64) BigDec {
// TODO: use mutable functions here
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)
}
31 changes: 31 additions & 0 deletions osmomath/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ var (
one_half sdk.Dec = sdk.MustNewDecFromStr("0.5")
one sdk.Dec = sdk.OneDec()
two sdk.Dec = sdk.MustNewDecFromStr("2")

// https://www.wolframalpha.com/input?i=2.718281828459045235360287471352662498&assumption=%22ClashPrefs%22+-%3E+%7B%22Math%22%7D
eulersNumber = MustNewDecFromStr("2.718281828459045235360287471352662498")
)

// Returns the internal "power precision".
Expand Down Expand Up @@ -171,3 +174,31 @@ func PowApprox(base sdk.Dec, exp sdk.Dec, precision sdk.Dec) sdk.Dec {
}
return sum
}

// TODO: spec
func Exp(exponent BigDec) BigDec {

// let exponent = i + f where i is the integer part and f is the fractional part.
// then e^exponent = e^i * e^f

integer := exponent.TruncateDec()
fractional := exponent.Sub(integer)

integerExp := eulersNumber.Power(uint64(integer.TruncateInt64()))

fractionalExp := expApprox(eulersNumber, fractional)

return integerExp.Add(fractionalExp)
}

// expApprox computes the exponential of a base.
// 0 < exponent < 1
// exp(0) = 1
// exp(1) = e
// Find Chebyshev nodes using Remez algorithm:
// https://github.com/samhocevar/lolremez/wiki/Tutorial-1-of-5%3A-exp%28x%29-the-quick-way
func expApprox(base BigDec, exponent BigDec) BigDec {
return ZeroDec()
}

// Step 1: find Chebushev
28 changes: 26 additions & 2 deletions osmomath/math_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"

"github.com/osmosis-labs/osmosis/v13/app/apptesting/osmoassert"

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

func TestAbsDifferenceWithSign(t *testing.T) {
Expand Down Expand Up @@ -166,3 +165,28 @@ func TestPow(t *testing.T) {
})
}
}

func TestExp(t *testing.T) {

tests := map[string]struct {
exponent BigDec
expectedResult BigDec
expectError bool
}{
// The value below is log base 2 of Max Spot Price (2^128 - 1)
"exp(32) = 78962960182680.695160978022635108224219956195115352": {
exponent: NewBigDec(32),
// https://www.wolframalpha.com/input?i=exp%2832%29+50+sig+figs
expectedResult: MustNewDecFromStr("78962960182680.695160978022635108224219956195115352"),
},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {

result := Exp(tc.exponent)

require.Equal(t, tc.expectedResult, result)
})
}
}

0 comments on commit 90f137e

Please sign in to comment.