Documentation ¶
Index ¶
- Constants
- Variables
- func GetSortedElementsByDecWeightDesc[K cmp.Ordered](m map[K]*Dec) []K
- func GetSortedKeys[K cmp.Ordered, V any](m map[K]V) []K
- func InDelta(expected, result Dec, epsilon Dec) (bool, error)
- func SlicesInDelta(a, b []Dec, epsilon Dec) (bool, error)
- func TestDecReduce(t *testing.T)
- type Dec
- func CalcEma(alpha, current, previous Dec, firstTime bool) (Dec, error)
- func CumulativeSum(arr []Dec) ([]Dec, error)
- func Exp(x Dec) (Dec, error)
- func Exp10(x Dec) (Dec, error)
- func GetQuantileOfDecs(decs []Dec, quantile Dec) (Dec, error)
- func Gradient(p, c, x Dec) (Dec, error)
- func LinearInterpolation(x, xp, fp []Dec) ([]Dec, error)
- func Ln(x Dec) (Dec, error)
- func Log10(x Dec) (Dec, error)
- func Max(x Dec, y Dec) (Dec, error)
- func Median(data []Dec) (Dec, error)
- func Min(x Dec, y Dec) (Dec, error)
- func MustNewDecFromString(s string) Dec
- func NewDecFinite(coeff int64, exp int32) Dec
- func NewDecFromInt64(x int64) Dec
- func NewDecFromSdkInt(x sdkmath.Int) (Dec, error)
- func NewDecFromSdkLegacyDec(x sdkmath.LegacyDec) (Dec, error)
- func NewDecFromString(s string) (Dec, error)
- func NewDecFromUint64(x uint64) (Dec, error)
- func NewNaN() Dec
- func NewNonNegativeDecFromString(s string) (Dec, error)
- func NewNonNegativeFixedDecFromString(s string, max uint32) (Dec, error)
- func NewPositiveDecFromString(s string) (Dec, error)
- func NewPositiveFixedDecFromString(s string, max uint32) (Dec, error)
- func OneDec() Dec
- func Phi(p, c, x Dec) (Dec, error)
- func Pow(x Dec, y Dec) (Dec, error)
- func StdDev(data []Dec) (Dec, error)
- func SumDecSlice(x []Dec) (Dec, error)
- func WeightedPercentile(data, weights, percentiles []Dec) ([]Dec, error)
- func ZeroDec() Dec
- func (x Dec) Abs() (Dec, error)
- func (x Dec) Add(y Dec) (Dec, error)
- func (x Dec) BigInt() (*big.Int, error)
- func (x Dec) Ceil() (Dec, error)
- func (x Dec) Cmp(y Dec) int
- func (x Dec) Coeff() (big.Int, error)
- func (x Dec) Equal(y Dec) bool
- func (x Dec) Floor() (Dec, error)
- func (x Dec) Gt(y Dec) bool
- func (x Dec) Gte(y Dec) bool
- func (x Dec) Int64() (int64, error)
- func (x Dec) IsFinite() bool
- func (x Dec) IsNaN() bool
- func (x Dec) IsNegative() bool
- func (x Dec) IsPositive() bool
- func (x Dec) IsZero() bool
- func (x Dec) Lt(y Dec) bool
- func (x Dec) Lte(y Dec) bool
- func (x Dec) Marshal() ([]byte, error)
- func (x Dec) MarshalJSON() ([]byte, error)
- func (x *Dec) MarshalTo(data []byte) (n int, err error)
- func (x Dec) Mul(y Dec) (Dec, error)
- func (x Dec) MulExact(y Dec) (Dec, error)
- func (x Dec) Neg() (Dec, error)
- func (x Dec) NumDecimalPlaces() uint32
- func (x Dec) Quo(y Dec) (Dec, error)
- func (x Dec) QuoExact(y Dec) (Dec, error)
- func (x Dec) QuoInteger(y Dec) (Dec, error)
- func (x Dec) Reduce() (Dec, int)
- func (x Dec) Rem(y Dec) (Dec, error)
- func (x Dec) SdkIntTrim() (sdkmath.Int, error)
- func (x Dec) SdkLegacyDec() (sdkmath.LegacyDec, error)
- func (x Dec) Size() int
- func (x Dec) Sqrt() (Dec, error)
- func (x Dec) String() string
- func (x Dec) Sub(y Dec) (Dec, error)
- func (x Dec) UInt64() (uint64, error)
- func (x *Dec) Unmarshal(data []byte) error
- func (x *Dec) UnmarshalJSON(bz []byte) error
Constants ¶
const ( GreaterThan = 1 LessThan = -1 EqualTo = 0 )
constants for more convenient intent behind dec.Cmp values.
const MaxBitLen = 256
MaxBitLen defines the maximum bit length supported bit Int and Uint types.
const NaNStr = "NaN"
Variables ¶
var ( ErrInvalidDecString = errorsmod.Register(mathCodespace, 1, "invalid decimal string") ErrUnexpectedRounding = errorsmod.Register(mathCodespace, 2, "unexpected rounding") ErrNonIntegral = errorsmod.Register(mathCodespace, 3, "value is non-integral") ErrInfiniteString = errorsmod.Register(mathCodespace, 4, "value is infinite") ErrOverflow = errorsmod.Register(mathCodespace, 5, "overflow") ErrNaN = errorsmod.Register(mathCodespace, 6, "NaN not permitted in this context") ErrNotMatchingLength = errorsmod.Register(mathCodespace, 7, "slices are not of the same length") ErrOutOfRange = errorsmod.Register(mathCodespace, 8, "value is out of range") )
var DecValue collcodec.ValueCodec[Dec] = decValueCodec{}
var LegacyDecValue collcodec.ValueCodec[cosmosMath.LegacyDec] = legacyDecValueCodec{}
Functions ¶
func GetSortedElementsByDecWeightDesc ¶
Generic function that sorts the keys of a map. Used for deterministic ranging of arrays with weights in a map the keys are sorted by the value they map to, in descending order whose keys may not include some values in the array. When an array element is not in the map, it is not included in the output array.
func GetSortedKeys ¶
Generic function that sorts the keys of a map Used for deterministic ranging of maps
func SlicesInDelta ¶
Helper function to compare two slices of alloraMath.Dec within a delta
func TestDecReduce ¶ added in v0.4.0
This function cannot run in the math_test package because it needs direct access to the apd.Coeff field
Types ¶
type Dec ¶
type Dec struct {
// contains filtered or unexported fields
}
Dec is a wrapper struct around apd.Decimal that does no mutation of apd.Decimal's when performing arithmetic, instead creating a new apd.Decimal for every operation ensuring usage is safe.
Using apd.Decimal directly can be unsafe because apd operations mutate the underlying Decimal, but when copying the big.Int structure can be shared between Decimal instances causing corruption. This was originally discovered in regen0-network/mainnet#15.
func CalcEma ¶
all exponential moving average functions take the form x_average=α*x_current + (1-α)*x_previous
this covers the equations Uij = αUij + (1 − α)Ui−1,j ̃Vik = αVik + (1 − α)Vi−1,k ̃Wim = αWim + (1 − α)Wi−1,m
func CumulativeSum ¶ added in v0.2.13
CumulativeSum calculates the cumulative sum of an array of Dec values.
func GetQuantileOfDecs ¶ added in v0.6.0
Compute the nth percentile of the decs array
func LinearInterpolation ¶ added in v0.2.13
LinearInterpolation performs linear interpolation on Dec values.
func Log10 ¶
Log10 returns a new Dec with the value of the base 10 logarithm of x, without mutating x.
func MustNewDecFromString ¶
MustNewDecFromString returns a new Dec from a given string. It panics if the string cannot be parsed. The string should be in the format of `123.456`.
func NewDecFinite ¶
NewDecFinite returns a decimal with a value of coeff * 10^exp.
func NewDecFromSdkInt ¶
NewDec takes a cosmos `sdkmath.Int` and turns it into a Dec it converts via strings and throws an error if the string is unable to be parsed
func NewDecFromSdkLegacyDec ¶
NewDec takes a cosmos `sdkmath.LegacyDec` and turns it into a Dec it converts via strings and throws an error if the string is unable to be parsed
func NewDecFromString ¶
NewDecFromString returns a new Dec from a given string. It returns an error if the string cannot be parsed. The string should be in the format of `123.456`.
func NewDecFromUint64 ¶
create a new dec from a uint64 value it converts via strings and throws an error if the string is unable to be parsed
func NewNonNegativeDecFromString ¶
NewNonNegativeDecFromString returns a new Dec from a given string. It returns an error if the string cannot be parsed or if the decimal is negative. The string should be in the format of `123.456`.
func NewNonNegativeFixedDecFromString ¶
NewNonNegativeFixedDecFromString returns a new Dec from a given string and an upper limit on the number of decimal places. It returns an error if the string cannot be parsed, if the decimal is negative, or if the number of decimal places exceeds the maximum. The string should be in the format of `123.456`.
func NewPositiveDecFromString ¶
NewPositiveDecFromString returns a new Dec from a string, returning an error if the string cannot be parsed or if the decimal is not positive. The string should be in the format of `123.456`.
func NewPositiveFixedDecFromString ¶
NewPositiveFixedDecFromString takes a string and an upper limit on the number of decimal places, returning a Dec or error if the string cannot be parsed, if the decimal is not positive, or if the number of decimal places exceeds the maximum. The string should be in the format of `123.456`.
func StdDev ¶
StdDev calculates the standard deviation of a slice of `Dec` stdDev = sqrt((Σ(x - μ))^2/ N) where μ is mean and N is number of elements
func SumDecSlice ¶
Generic Sum function, given an array of values returns its sum
func WeightedPercentile ¶ added in v0.2.13
WeightedPercentile calculates weighted percentiles of Dec values.
func (Dec) Add ¶
Add returns a new Dec with value `x+y` without mutating any argument and error if there is an overflow or resultant NaN.
func (Dec) BigInt ¶
BigInt converts x to a *big.Int or returns an error if x cannot fit precisely into an *big.Int.
func (Dec) Ceil ¶
Ceil returns a new Dec with the value of x rounded up to the nearest integer, without mutating x.
func (Dec) Cmp ¶
Cmp compares x and y and returns: -1 if x < y 0 if x == y +1 if x > y undefined if d or x are NaN
func (Dec) Floor ¶
Floor returns a new Dec with the value of x rounded down to the nearest integer, without mutating x.
func (Dec) Int64 ¶
Int64 converts x to an int64 or returns an error if x cannot fit precisely into an int64.
func (Dec) IsNegative ¶
IsNegative returns true if the decimal is negative.
func (Dec) IsPositive ¶
IsPositive returns true if the decimal is positive.
func (Dec) MarshalJSON ¶
MarshalJSON marshals the decimal
func (Dec) Mul ¶
Mul returns a new Dec with value `x*y` (formatted as decimal128, with 34 digit precision) without mutating any argument and error if there is an overflow.
func (Dec) MulExact ¶
MulExact returns a new dec with value x * y. The product must not be rounded or ErrUnexpectedRounding will be returned.
func (Dec) Neg ¶
Neg negates the decimal and returns a new Dec with value `-x` without mutating any argument and error if there is an overflow.
func (Dec) NumDecimalPlaces ¶
NumDecimalPlaces returns the number of decimal places in x.
func (Dec) Quo ¶
Quo returns a new Dec with value `x/y` (formatted as decimal128, 34 digit precision) without mutating any argument and error if there is an overflow.
func (Dec) QuoExact ¶
QuoExact is a version of Quo that returns ErrUnexpectedRounding if any rounding occurred.
func (Dec) QuoInteger ¶
QuoInteger returns a new integral Dec with value `x/y` (formatted as decimal128, with 34 digit precision) without mutating any argument and error if there is an overflow.
func (Dec) Reduce ¶
Reduce returns a copy of x with all trailing zeros removed and the number of trailing zeros removed.
func (Dec) Rem ¶
Rem returns the integral remainder from `x/y` (formatted as decimal128, with 34 digit precision) without mutating any argument and error if the integer part of x/y cannot fit in 34 digit precision
func (Dec) SdkIntTrim ¶
SdkIntTrim rounds decimal number to the integer towards zero and converts it to `sdkmath.Int`. returns error if the Dec is not representable in an sdkmath.Int
func (Dec) SdkLegacyDec ¶
SdkLegacyDec converts Dec to `sdkmath.LegacyDec` can return nil if the value is not representable in a LegacyDec
func (Dec) Sqrt ¶
Sqrt returns a new Dec with the value of the square root of x, without mutating x.
func (Dec) Sub ¶
Sub returns a new Dec with value `x-y` without mutating any argument and error if there is an overflow.
func (Dec) UInt64 ¶
Int64 converts x to an int64 or returns an error if x cannot fit precisely into an int64.
func (*Dec) UnmarshalJSON ¶
UnmarshalJSON defines custom decoding scheme