math

package
v0.3.0-upgrade Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 8, 2024 License: Apache-2.0 Imports: 10 Imported by: 6

Documentation

Index

Constants

View Source
const (
	GreaterThan = 1
	LessThan    = -1
	EqualTo     = 0
)

constants for more convenient intent behind dec.Cmp values.

Variables

View Source
var (
	ErrInvalidDecString   = errors.Register(mathCodespace, 1, "invalid decimal string")
	ErrUnexpectedRounding = errors.Register(mathCodespace, 2, "unexpected rounding")
	ErrNonIntegeral       = errors.Register(mathCodespace, 3, "value is non-integral")
	ErrInfiniteString     = errors.Register(mathCodespace, 4, "value is infinite")
)
View Source
var DecValue collcodec.ValueCodec[Dec] = decValueCodec{}
View Source
var LegacyDecValue collcodec.ValueCodec[cosmosMath.LegacyDec] = legacyDecValueCodec{}

Functions

func GetSortedElementsByDecWeightDesc

func GetSortedElementsByDecWeightDesc[K cmp.Ordered](l []K, m map[K]*Dec) []K

Generic function that sorts the keys of a map. Used for deterministic ranging of arrays with weights in a map 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

func GetSortedKeys[K cmp.Ordered, V any](m map[K]V) []K

Generic function that sorts the keys of a map Used for deterministic ranging of maps

func InDelta

func InDelta(expected, result Dec, epsilon Dec) bool

helper function for test suites that want to check if some math is within a delta

func SlicesInDelta

func SlicesInDelta(a, b []Dec, epsilon Dec) bool

Helper function to compare two slices of alloraMath.Dec within a delta

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

func CalcEma(
	alpha,
	current,
	previous Dec,
	firstTime bool,
) (Dec, error)

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 CalcExpDecay added in v0.2.6

func CalcExpDecay(
	currentRev,
	decayFactor Dec,
) (Dec, error)

func CumulativeSum

func CumulativeSum(arr []Dec) ([]Dec, error)

CumulativeSum calculates the cumulative sum of an array of Dec values.

func Exp

func Exp(x Dec) (Dec, error)

Exp returns a new Dec with the value of e^x, without mutating x.

func Exp10

func Exp10(x Dec) (Dec, error)

Exp10 returns a new Dec with the value of 10^x, without mutating x.

func Gradient

func Gradient(p, c, x Dec) (Dec, error)

Implements the new gradient function phi prime φ'_p(x) = p / (exp(p * (c - x)) + 1)

func LinearInterpolation

func LinearInterpolation(x, xp, fp []Dec) ([]Dec, error)

LinearInterpolation performs linear interpolation on Dec values.

func Ln

func Ln(x Dec) (Dec, error)

Ln returns a new Dec with the value of the natural logarithm of x, without mutating x.

func Log10

func Log10(x Dec) (Dec, error)

Log10 returns a new Dec with the value of the base 10 logarithm of x, without mutating x.

func Max

func Max(x Dec, y Dec) Dec

returns the max of x and y without mutating x or y.

func Median

func Median(data []Dec) (Dec, error)

Median calculates the median of a slice of `Dec`

func Min

func Min(x Dec, y Dec) Dec

returns the min of x and y without mutating x or y.

func MustNewDecFromString

func MustNewDecFromString(s string) Dec

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

func NewDecFinite(coeff int64, exp int32) Dec

NewDecFinite returns a decimal with a value of coeff * 10^exp.

func NewDecFromInt64

func NewDecFromInt64(x int64) Dec

create a new dec from an int64 value

func NewDecFromSdkInt

func NewDecFromSdkInt(x sdkmath.Int) (Dec, error)

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

func NewDecFromSdkLegacyDec(x sdkmath.LegacyDec) (Dec, error)

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

func NewDecFromString(s string) (Dec, error)

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

func NewDecFromUint64(x uint64) (Dec, error)

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 NewNaN

func NewNaN() Dec

create a new Dec that represents NaN

func NewNonNegativeDecFromString

func NewNonNegativeDecFromString(s string) (Dec, error)

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

func NewNonNegativeFixedDecFromString(s string, max uint32) (Dec, error)

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

func NewPositiveDecFromString(s string) (Dec, error)

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

func NewPositiveFixedDecFromString(s string, max uint32) (Dec, error)

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 OneDec

func OneDec() Dec

The number 1 encoded as Dec

func Phi

func Phi(p, c, x Dec) (Dec, error)

Implements the potential function phi for the module ϕ_p(x) = ln(1 + e^(p * (x - c)))

func Pow

func Pow(x Dec, y Dec) (Dec, error)

Pow returns a new Dec with the value of x**y, without mutating x or y.

func StdDev

func StdDev(data []Dec) (Dec, error)

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

func SumDecSlice(x []Dec) (Dec, error)

Generic Sum function, given an array of values returns its sum

func WeightedPercentile

func WeightedPercentile(data, weights, percentiles []Dec) ([]Dec, error)

WeightedPercentile calculates weighted percentiles of Dec values.

func ZeroDec

func ZeroDec() Dec

The number 0 encoded as Dec

func (Dec) Abs

func (x Dec) Abs() Dec

Abs returns a new Dec with the absolute value of x, without mutating x.

func (Dec) Add

func (x Dec) Add(y Dec) (Dec, error)

Add returns a new Dec with value `x+y` without mutating any argument and error if there is an overflow.

func (Dec) BigInt

func (x Dec) BigInt() (*big.Int, error)

BigInt converts x to a *big.Int or returns an error if x cannot fit precisely into an *big.Int.

func (Dec) Ceil

func (x Dec) Ceil() (Dec, error)

Ceil returns a new Dec with the value of x rounded up to the nearest integer, without mutating x.

func (Dec) Cmp

func (x Dec) Cmp(y Dec) int

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) Coeff

func (x Dec) Coeff() big.Int

func (Dec) Equal

func (x Dec) Equal(y Dec) bool

func (Dec) Floor

func (x Dec) Floor() (Dec, error)

Floor returns a new Dec with the value of x rounded down to the nearest integer, without mutating x.

func (Dec) Gt

func (x Dec) Gt(y Dec) bool

func (Dec) Gte

func (x Dec) Gte(y Dec) bool

func (Dec) Int64

func (x Dec) Int64() (int64, error)

Int64 converts x to an int64 or returns an error if x cannot fit precisely into an int64.

func (Dec) IsFinite

func (x Dec) IsFinite() bool

IsFinite returns true if the decimal is finite.

func (Dec) IsNaN

func (x Dec) IsNaN() bool

func (Dec) IsNegative

func (x Dec) IsNegative() bool

IsNegative returns true if the decimal is negative.

func (Dec) IsPositive

func (x Dec) IsPositive() bool

IsPositive returns true if the decimal is positive.

func (Dec) IsZero

func (x Dec) IsZero() bool

IsZero returns true if the decimal is zero.

func (Dec) Lt

func (x Dec) Lt(y Dec) bool

func (Dec) Lte

func (x Dec) Lte(y Dec) bool

func (Dec) Marshal

func (d Dec) Marshal() ([]byte, error)

Marshal implements the gogo proto custom type interface.

func (Dec) MarshalJSON

func (d Dec) MarshalJSON() ([]byte, error)

MarshalJSON marshals the decimal

func (*Dec) MarshalTo

func (d *Dec) MarshalTo(data []byte) (n int, err error)

MarshalTo implements the gogo proto custom type interface.

func (Dec) Mul

func (x Dec) Mul(y Dec) (Dec, error)

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

func (x Dec) MulExact(y Dec) (Dec, error)

MulExact returns a new dec with value x * y. The product must not round or ErrUnexpectedRounding will be returned.

func (Dec) Neg

func (x Dec) Neg() (Dec, error)

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

func (x Dec) NumDecimalPlaces() uint32

NumDecimalPlaces returns the number of decimal places in x.

func (Dec) Quo

func (x Dec) Quo(y Dec) (Dec, error)

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

func (x Dec) QuoExact(y Dec) (Dec, error)

QuoExact is a version of Quo that returns ErrUnexpectedRounding if any rounding occurred.

func (Dec) QuoInteger

func (x Dec) QuoInteger(y Dec) (Dec, error)

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

func (x Dec) Reduce() (Dec, int)

Reduce returns a copy of x with all trailing zeros removed and the number of trailing zeros removed.

func (Dec) Rem

func (x Dec) Rem(y Dec) (Dec, error)

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

func (x Dec) SdkIntTrim() sdkmath.Int

SdkIntTrim rounds decimal number to the integer towards zero and converts it to `sdkmath.Int`. Panics if x is bigger the SDK Int max value

func (Dec) SdkLegacyDec

func (x Dec) SdkLegacyDec() sdkmath.LegacyDec

SdkLegacyDec converts Dec to `sdkmath.LegacyDec` can return nil if the value is not representable in a LegacyDec

func (Dec) Size

func (d Dec) Size() int

Size returns the size of the marshalled Dec type in bytes

func (Dec) Sqrt

func (x Dec) Sqrt() (Dec, error)

Sqrt returns a new Dec with the value of the square root of x, without mutating x.

func (Dec) String

func (x Dec) String() string

func (Dec) Sub

func (x Dec) Sub(y Dec) (Dec, error)

Sub returns a new Dec with value `x-y` without mutating any argument and error if there is an overflow.

func (Dec) UInt64

func (x Dec) UInt64() (uint64, error)

Int64 converts x to an int64 or returns an error if x cannot fit precisely into an int64.

func (*Dec) Unmarshal

func (d *Dec) Unmarshal(data []byte) error

Unmarshal implements the gogo proto custom type interface.

func (*Dec) UnmarshalJSON

func (d *Dec) UnmarshalJSON(bz []byte) error

UnmarshalJSON defines custom decoding scheme

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL