math

package
v0.6.6 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2024 License: Apache-2.0 Imports: 14 Imported by: 1

Documentation

Index

Constants

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

constants for more convenient intent behind dec.Cmp values.

View Source
const MaxBitLen = 256

MaxBitLen defines the maximum bit length supported bit Int and Uint types.

View Source
const NaNStr = "NaN"

Variables

View Source
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")
)
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](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 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

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, error)

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, error)

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

func TestDecReduce added in v0.4.0

func TestDecReduce(t *testing.T)

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

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 CumulativeSum added in v0.2.13

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 GetQuantileOfDecs added in v0.6.0

func GetQuantileOfDecs(
	decs []Dec,
	quantile Dec,
) (Dec, error)

Compute the nth percentile of the decs array

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

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, error)

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

func Median added in v0.2.8

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

Median calculates the median of a slice of `Dec`

func Min

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

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

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, error)

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 or resultant NaN.

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, error)

Coeff copies x into a big int while minimizing trailing zeroes

func (Dec) Equal

func (x Dec) Equal(y Dec) bool

Equal returns true if x and y are equal.

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

is x greater than y

func (Dec) Gte

func (x Dec) Gte(y Dec) bool

is x greater than or equal to y

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

IsNaN returns true if the decimal is not a number.

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

is x less than y

func (Dec) Lte

func (x Dec) Lte(y Dec) bool

is x less than or equal to y

func (Dec) Marshal

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

Marshal implements the gogo proto custom type interface.

func (Dec) MarshalJSON

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

MarshalJSON marshals the decimal

func (*Dec) MarshalTo

func (x *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 be rounded 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, error)

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

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

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

func (Dec) Size

func (x 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 (x *Dec) Unmarshal(data []byte) error

Unmarshal implements the gogo proto custom type interface.

func (*Dec) UnmarshalJSON

func (x *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