ixomath

package
v4.0.0-rc.0 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

This file creates type and function aliases the sdkmath.LegacyDec This is done for reducing verbosity and improving readability.

For consistency, we also alias Int and Uint so that sdkmath does not have to be directly imported in files where both decimal and integer types are used.

Index

Constants

View Source
const (
	// number of decimal places
	BigDecPrecision = 36

	// bytes required to represent the above precision
	// Ceiling[Log2[10**Precision - 1]]
	BigDecimalPrecisionBits = 120
)
View Source
const (
	DecPrecision         = sdkmath.LegacyPrecision
	DecimalPrecisionBits = sdkmath.LegacyDecimalPrecisionBits
)

Variables

View Source
var (
	ErrEmptyDecimalStr      = errors.New("decimal string cannot be empty")
	ErrInvalidDecimalLength = errors.New("invalid decimal length")
	ErrInvalidDecimalStr    = errors.New("invalid decimal string")
)

Decimal errors

View Source
var (
	// Dec
	NewDec                   = sdkmath.LegacyNewDec
	NewDecWithPrec           = sdkmath.LegacyNewDecWithPrec
	NewDecFromBigInt         = sdkmath.LegacyNewDecFromBigInt
	NewDecFromBigIntWithPrec = sdkmath.LegacyNewDecFromBigIntWithPrec
	NewDecFromInt            = sdkmath.LegacyNewDecFromInt
	NewDecFromIntWithPrec    = sdkmath.LegacyNewDecFromIntWithPrec
	NewDecFromStr            = sdkmath.LegacyNewDecFromStr
	MustNewDecFromStr        = sdkmath.LegacyMustNewDecFromStr
	ZeroDec                  = sdkmath.LegacyZeroDec
	OneDec                   = sdkmath.LegacyOneDec
	SmallestDec              = sdkmath.LegacySmallestDec

	// Int
	NewInt            = sdkmath.NewInt
	NewIntFromUint64  = sdkmath.NewIntFromUint64
	NewIntFromBigInt  = sdkmath.NewIntFromBigInt
	NewIntFromString  = sdkmath.NewIntFromString
	NewIntWithDecimal = sdkmath.NewIntWithDecimal
	ZeroInt           = sdkmath.ZeroInt
	OneInt            = sdkmath.OneInt
	IntEq             = sdkmath.IntEq
	MinInt            = sdkmath.MinInt
	MaxInt            = sdkmath.MaxInt

	// Uint
	NewUint           = sdkmath.NewUint
	NewUintFromString = sdkmath.NewUintFromString

	MinDec = sdkmath.LegacyMinDec
	MaxDec = sdkmath.LegacyMaxDec
)

Functions

func BigIntEq

func BigIntEq(t *testing.T, exp, got BigInt) (*testing.T, bool, string, string, string)

intended to be used with require/assert: require.True(IntEq(...)) nolint

func DecApproxEq

func DecApproxEq(t *testing.T, d1 BigDec, d2 BigDec, tol BigDec) (*testing.T, bool, string, string, string)

DecApproxEq returns true if the differences between two given decimals are smaller than the tolerance range. Intended to be used with require/assert: require.True(t, DecEq(...))

nolint

func DecEq

func DecEq(t *testing.T, exp, got BigDec) (*testing.T, bool, string, string, string)

DecEq returns true if two given decimals are equal. Intended to be used with require/assert: require.True(t, DecEq(...))

nolint

func DecsEqual

func DecsEqual(d1s, d2s []BigDec) bool

DecsEqual tests if two decimal arrays are equal

func Equal

func Equal[T Stringer](t *testing.T, tolerance ErrTolerance, A, B T)

nolint:thelper

func OrderOfMagnitude

func OrderOfMagnitude(num Dec) int

OrderOfMagnitude calculates the order of magnitude without using logarithms. CONTRACT: num must be positive or zero. Panics if not

Types

type BigDec

type BigDec struct {
	// contains filtered or unexported fields
}

NOTE: never use new(BigDec) or else we will panic unmarshalling into the nil embedded big.Int

func BigDecFromDec

func BigDecFromDec(d Dec) BigDec

BigDecFromDec returns the BigDec representation of an Dec. Values in any additional decimal places are truncated.

func BigDecFromDecCoinSlice

func BigDecFromDecCoinSlice(ds []sdk.DecCoin) []BigDec

BigDecFromDecSlice returns the []BigDec representation of an []Dec. Values in any additional decimal places are truncated.

func BigDecFromDecMut

func BigDecFromDecMut(d Dec) BigDec

BigDecFromDec returns the BigDec representation of an Dec. Values in any additional decimal places are truncated.

func BigDecFromDecSlice

func BigDecFromDecSlice(ds []Dec) []BigDec

BigDecFromDecSlice returns the []BigDec representation of an []Dec. Values in any additional decimal places are truncated.

func BigDecFromSDKInt

func BigDecFromSDKInt(i Int) BigDec

BigDecFromSDKInt returns the BigDec representation of an sdkInt. Values in any additional decimal places are truncated.

func BinarySearchBigDec

func BinarySearchBigDec(f func(BigDec) BigDec,
	lowerbound BigDec,
	upperbound BigDec,
	targetOutput BigDec,
	errTolerance ErrTolerance,
	maxIterations int,
) (BigDec, error)

BinarySearchBigDec takes as input: * an input range [lowerbound, upperbound] * an increasing function f * a target output x * max number of iterations (for gas control / handling does-not-converge cases)

It binary searches on the input range, until it finds an input y s.t. f(y) meets the err tolerance constraints for how close it is to x. If we perform more than maxIterations (or equivalently lowerbound = upperbound), we return an error.

func DivCoinAmtsByU64ToBigDec

func DivCoinAmtsByU64ToBigDec(coins []sdk.Coin, scales []uint64, round RoundingDirection) ([]BigDec, error)

func DivIntByU64ToBigDec

func DivIntByU64ToBigDec(i Int, u uint64, round RoundingDirection) (BigDec, error)

func Exp2

func Exp2(exponent BigDec) BigDec

Exp2 takes 2 to the power of a given non-negative decimal exponent and returns the result. The computation is performed by using th following property: 2^decimal_exp = 2^{integer_exp + fractional_exp} = 2^integer_exp * 2^fractional_exp The max supported exponent is defined by the global maxSupportedExponent. If a greater exponent is given, the function panics. Panics if the exponent is negative. The answer is correct up to a factor of 10^-18. Meaning, result = result * k for k in [1 - 10^(-18), 1 + 10^(-18)] Note: our Python script plots show accuracy up to a factor of 10^22. However, in Go tests we only test up to 10^18. Therefore, this is the guarantee.

func MaxBigDec

func MaxBigDec(d1, d2 BigDec) BigDec

MaxBigDec gets maximum decimal between two

func MinBigDec

func MinBigDec(d1, d2 BigDec) BigDec

MinBigDec gets minimum decimal between two

func MonotonicSqrtBigDec

func MonotonicSqrtBigDec(d BigDec) (BigDec, error)

func MonotonicSqrtBigDecMut

func MonotonicSqrtBigDecMut(d BigDec) (BigDec, error)

func MustMonotonicSqrtBigDec

func MustMonotonicSqrtBigDec(d BigDec) BigDec

MustMonotonicSqrt returns the output of MonotonicSqrt, panicking on error.

func MustNewBigDecFromStr

func MustNewBigDecFromStr(s string) BigDec

Decimal from string, panic on error

func NewBigDec

func NewBigDec(i int64) BigDec

create a new NewBigDec from integer assuming whole number

func NewBigDecFromBigInt

func NewBigDecFromBigInt(i *big.Int) BigDec

create a new BigDec from big integer assuming whole numbers CONTRACT: prec <= BigDecPrecision

func NewBigDecFromBigIntMut

func NewBigDecFromBigIntMut(i *big.Int) BigDec

func NewBigDecFromBigIntMutWithPrec

func NewBigDecFromBigIntMutWithPrec(i *big.Int, prec int64) BigDec

returns a BigDec, built using a mutated copy of the passed in bigint. CONTRACT: prec <= BigDecPrecision

func NewBigDecFromBigIntWithPrec

func NewBigDecFromBigIntWithPrec(i *big.Int, prec int64) BigDec

create a new BigDec from big integer assuming whole numbers CONTRACT: prec <= BigDecPrecision

func NewBigDecFromDecMulDec

func NewBigDecFromDecMulDec(a, b Dec) BigDec

func NewBigDecFromInt

func NewBigDecFromInt(i BigInt) BigDec

create a new BigDec from big integer assuming whole numbers CONTRACT: prec <= BigDecPrecision

func NewBigDecFromIntWithPrec

func NewBigDecFromIntWithPrec(i BigInt, prec int64) BigDec

create a new BigDec from big integer with decimal place at prec CONTRACT: prec <= BigDecPrecision

func NewBigDecFromStr

func NewBigDecFromStr(str string) (BigDec, error)

create a decimal from an input decimal string. valid must come in the form:

(-) whole integers (.) decimal integers

examples of acceptable input include:

-123.456
456.7890
345
-456789

NOTE - An error will return if more decimal places are provided in the string than the constant Precision.

CONTRACT - This function does not mutate the input str.

func NewBigDecWithPrec

func NewBigDecWithPrec(i, prec int64) BigDec

create a new BigDec from integer with decimal place at prec CONTRACT: prec <= BigDecPrecision

func OneBigDec

func OneBigDec() BigDec

func SmallestBigDec

func SmallestBigDec() BigDec

func ZeroBigDec

func ZeroBigDec() BigDec

func (BigDec) Abs

func (d BigDec) Abs() BigDec

nolint: stylecheck

func (BigDec) AbsMut

func (d BigDec) AbsMut() BigDec

func (BigDec) Add

func (d BigDec) Add(d2 BigDec) BigDec

addition

func (BigDec) AddMut

func (d BigDec) AddMut(d2 BigDec) BigDec

mutative addition

func (BigDec) ApproxRoot

func (d BigDec) ApproxRoot(root uint64) (guess BigDec, err error)

ApproxRoot returns an approximate estimation of a Dec's positive real nth root using Newton's method (where n is positive). The algorithm starts with some guess and computes the sequence of improved guesses until an answer converges to an approximate answer. It returns `|d|.ApproxRoot() * -1` if input is negative. A maximum number of 100 iterations is used a backup boundary condition for cases where the answer never converges enough to satisfy the main condition.

func (BigDec) ApproxSqrt

func (d BigDec) ApproxSqrt() (BigDec, error)

ApproxSqrt is a wrapper around ApproxRoot for the common special case of finding the square root of a number. It returns -(sqrt(abs(d)) if input is negative. TODO: Optimize this to be faster just using native big int sqrt.

func (BigDec) BigInt

func (d BigDec) BigInt() *big.Int

BigInt returns a copy of the underlying big.Int.

func (BigDec) BigIntMut

func (d BigDec) BigIntMut() *big.Int

BigIntMut returns the pointer of the underlying big.Int.

func (BigDec) Ceil

func (d BigDec) Ceil() BigDec

Ceil returns the smallest integer value (as a decimal) that is greater than or equal to the given decimal.

func (BigDec) CeilMut

func (d BigDec) CeilMut() BigDec

func (*BigDec) ChopPrecision

func (d *BigDec) ChopPrecision(precision uint64) BigDec

ChopPrecision truncates all decimals after precision numbers after decimal point CONTRACT: precision <= BigDecPrecision Panics if precision exceeds BigDecPrecision

func (*BigDec) ChopPrecisionMut

func (d *BigDec) ChopPrecisionMut(precision uint64) BigDec

ChopPrecisionMut truncates all decimals after precision numbers after decimal point. Mutative CONTRACT: precision <= BigDecPrecision Panics if precision exceeds BigDecPrecision

func (BigDec) Clone

func (d BigDec) Clone() BigDec

Clone performs a deep copy of the receiver and returns the new result.

func (BigDec) CustomBaseLog

func (x BigDec) CustomBaseLog(base BigDec) BigDec

log_a(x) custom base logarithm Formula: log_a(b) = log_2(b) / log_2(a)

func (BigDec) Dec

func (d BigDec) Dec() Dec

Dec returns the osmomath.Dec representation of a BigDec. Values in any additional decimal places are truncated.

func (BigDec) DecRoundUp

func (d BigDec) DecRoundUp() Dec

DecRoundUp returns the osmomath.Dec representation of a BigDec. Round up at precision end. Values in any additional decimal places are truncated.

func (BigDec) DecWithPrecision

func (d BigDec) DecWithPrecision(precision uint64) Dec

DecWithPrecision converts BigDec to Dec with desired precision Example: BigDec: 1.010100000000153000000000000000000000 precision: 4 Output Dec: 1.010100000000000000 Panics if precision exceeds DecPrecision

func (BigDec) Equal

func (d BigDec) Equal(d2 BigDec) bool

func (BigDec) Float64

func (d BigDec) Float64() (float64, error)

Float64 returns the float64 representation of a BigDec. Will return the error if the conversion failed.

func (BigDec) Format

func (d BigDec) Format(s fmt.State, verb rune)

format decimal state

func (BigDec) GT

func (d BigDec) GT(d2 BigDec) bool

func (BigDec) GTE

func (d BigDec) GTE(d2 BigDec) bool

func (BigDec) IsInteger

func (d BigDec) IsInteger() bool

is integer, e.g. decimals are zero

func (BigDec) IsNegative

func (d BigDec) IsNegative() bool

func (BigDec) IsNil

func (d BigDec) IsNil() bool

func (BigDec) IsPositive

func (d BigDec) IsPositive() bool

func (BigDec) IsZero

func (d BigDec) IsZero() bool

func (BigDec) LT

func (d BigDec) LT(d2 BigDec) bool

func (BigDec) LTE

func (d BigDec) LTE(d2 BigDec) bool

func (BigDec) Ln

func (x BigDec) Ln() BigDec

Natural logarithm of x. Formula: ln(x) = log_2(x) / log_2(e)

func (BigDec) LogBase2

func (x BigDec) LogBase2() BigDec

LogBase2 returns log_2 {x}. Rounds down by truncations during division and right shifting. Accurate up to 32 precision digits. Implementation is based on: https://stm32duinoforum.com/forum/dsp/BinaryLogarithm.pdf

func (BigDec) Marshal

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

Marshal implements the gogo proto custom type interface.

func (BigDec) MarshalAmino

func (d BigDec) MarshalAmino() ([]byte, error)

Override Amino binary serialization by proxying to protobuf.

func (BigDec) MarshalJSON

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

MarshalJSON marshals the decimal

func (*BigDec) MarshalTo

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

MarshalTo implements the gogo proto custom type interface.

func (BigDec) MarshalYAML

func (d BigDec) MarshalYAML() (interface{}, error)

MarshalYAML returns the YAML representation.

func (BigDec) Mul

func (d BigDec) Mul(d2 BigDec) BigDec

Mut performs non-mutative multiplication. The receiver is not modifier but the result is.

func (BigDec) MulDec

func (d BigDec) MulDec(d2 Dec) BigDec

func (BigDec) MulDecMut

func (d BigDec) MulDecMut(d2 Dec) BigDec

func (BigDec) MulInt

func (d BigDec) MulInt(i BigInt) BigDec

multiplication

func (BigDec) MulInt64

func (d BigDec) MulInt64(i int64) BigDec

MulInt64 - multiplication with int64

func (BigDec) MulMut

func (d BigDec) MulMut(d2 BigDec) BigDec

Mut performs non-mutative multiplication. The receiver is not modifier but the result is.

func (BigDec) MulRoundUp

func (d BigDec) MulRoundUp(d2 BigDec) BigDec

multiplication round up

func (BigDec) MulRoundUpDec

func (d BigDec) MulRoundUpDec(d2 Dec) BigDec

multiplication round up by Dec

func (BigDec) MulTruncate

func (d BigDec) MulTruncate(d2 BigDec) BigDec

multiplication truncate

func (BigDec) MulTruncateDec

func (d BigDec) MulTruncateDec(d2 Dec) BigDec

func (BigDec) MustFloat64

func (d BigDec) MustFloat64() float64

MustFloat64 returns the float64 representation of a BigDec. Would panic if the conversion failed.

func (BigDec) Neg

func (d BigDec) Neg() BigDec

func (BigDec) NegMut

func (d BigDec) NegMut() BigDec

func (BigDec) Power

func (d BigDec) Power(power BigDec) BigDec

Power returns a result of raising the given big dec to a positive decimal power. Panics if the power is negative. Panics if the base is negative. Does not mutate the receiver. The max supported exponent is defined by the global maxSupportedExponent. If a greater exponent is given, the function panics. The error is not bounded but expected to be around 10^-18, use with care. See the underlying Exp2, LogBase2 and Mul for the details of their bounds. WARNING: This function is broken for base < 1. The reason is that logarithm function is negative between zero and 1, and the Exp2(k) is undefined for negative k. As a result, this function panics if called for d < 1.

func (BigDec) PowerInteger

func (d BigDec) PowerInteger(power uint64) BigDec

PowerInteger takes a given decimal to an integer power and returns the result. Non-mutative. Uses square and multiply algorithm for performing the calculation.

func (BigDec) PowerIntegerMut

func (d BigDec) PowerIntegerMut(power uint64) BigDec

PowerIntegerMut takes a given decimal to an integer power and returns the result. Mutative. Uses square and multiply algorithm for performing the calculation.

func (BigDec) Quo

func (d BigDec) Quo(d2 BigDec) BigDec

quotient

func (BigDec) QuoByDecRoundUp

func (d BigDec) QuoByDecRoundUp(d2 Dec) BigDec

quotient, round up

func (BigDec) QuoInt

func (d BigDec) QuoInt(i BigInt) BigDec

quotient

func (BigDec) QuoInt64

func (d BigDec) QuoInt64(i int64) BigDec

QuoInt64 - quotient with int64

func (BigDec) QuoMut

func (d BigDec) QuoMut(d2 BigDec) BigDec

mutative quotient

func (BigDec) QuoRaw

func (d BigDec) QuoRaw(d2 int64) BigDec

func (BigDec) QuoRoundUp

func (d BigDec) QuoRoundUp(d2 BigDec) BigDec

quotient, round up

func (BigDec) QuoRoundUpMut

func (d BigDec) QuoRoundUpMut(d2 BigDec) BigDec

quotient, round up (mutative)

func (BigDec) QuoRoundUpNextIntMut

func (d BigDec) QuoRoundUpNextIntMut(d2 BigDec) BigDec

quotient, round up to next integer (mutative)

func (BigDec) QuoTruncate

func (d BigDec) QuoTruncate(d2 BigDec) BigDec

quotient truncate

func (BigDec) QuoTruncateDec

func (d BigDec) QuoTruncateDec(d2 Dec) BigDec

quotient truncate

func (BigDec) QuoTruncateDecMut

func (d BigDec) QuoTruncateDecMut(d2 Dec) BigDec

quotient truncate (mutative)

func (BigDec) QuoTruncateMut

func (d BigDec) QuoTruncateMut(d2 BigDec) BigDec

quotient truncate (mutative)

func (BigDec) RoundInt

func (d BigDec) RoundInt() BigInt

RoundInt round the decimal using bankers rounding

func (BigDec) RoundInt64

func (d BigDec) RoundInt64() int64

RoundInt64 rounds the decimal using bankers rounding

func (*BigDec) Size

func (d *BigDec) Size() int

Size implements the gogo proto custom type interface.

func (BigDec) String

func (d BigDec) String() string

String returns a BigDec as a string.

func (BigDec) Sub

func (d BigDec) Sub(d2 BigDec) BigDec

subtraction

func (BigDec) SubMut

func (d BigDec) SubMut(d2 BigDec) BigDec

func (BigDec) TickLog

func (x BigDec) TickLog() BigDec

log_1.0001(x) "tick" base logarithm Formula: log_1.0001(b) = log_2(b) / log_2(1.0001)

func (BigDec) TruncateDec

func (d BigDec) TruncateDec() BigDec

TruncateDec truncates the decimals from the number and returns a Dec

func (BigDec) TruncateInt

func (d BigDec) TruncateInt() BigInt

TruncateInt truncates the decimals from the number and returns an Int

func (BigDec) TruncateInt64

func (d BigDec) TruncateInt64() int64

TruncateInt64 truncates the decimals from the number and returns an int64

func (*BigDec) Unmarshal

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

Unmarshal implements the gogo proto custom type interface.

func (*BigDec) UnmarshalAmino

func (d *BigDec) UnmarshalAmino(bz []byte) error

func (*BigDec) UnmarshalJSON

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

UnmarshalJSON defines custom decoding scheme

type BigInt

type BigInt struct {
	// contains filtered or unexported fields
}

Int wraps big.Int with a 257 bit range bound Checks overflow, underflow and division by zero Exists in range from -(2^256 - 1) to 2^256 - 1

func MaxBigInt

func MaxBigInt(i, i2 BigInt) BigInt

MaxBigInt returns the maximum between two integers.

func MinBigInt

func MinBigInt(i1, i2 BigInt) BigInt

return the minimum of the ints

func NewBigInt

func NewBigInt(n int64) BigInt

NewBigInt constructs Int from int64

func NewBigIntFromBigInt

func NewBigIntFromBigInt(i *big.Int) BigInt

NewBigIntFromBigInt constructs Int from big.Int. If the provided big.Int is nil, it returns an empty instance. This function panics if the bit length is > 256.

func NewBigIntFromString

func NewBigIntFromString(s string) (res BigInt, ok bool)

NewBigIntFromString constructs Int from string

func NewBigIntFromUint64

func NewBigIntFromUint64(n uint64) BigInt

NewBigIntFromUint64 constructs an Int from a uint64.

func NewBigIntWithDecimal

func NewBigIntWithDecimal(n int64, dec int) BigInt

NewBigIntWithDecimal constructs Int with decimal Result value is n*10^dec

func OneBigInt

func OneBigInt() BigInt

OneBigInt returns Int value with one

func ZeroBigInt

func ZeroBigInt() BigInt

ZeroBigInt returns Int value with zero

func (BigInt) Abs

func (i BigInt) Abs() BigInt

Abs returns the absolute value of Int.

func (BigInt) Add

func (i BigInt) Add(i2 BigInt) (res BigInt)

Add adds Int from another

func (BigInt) AddRaw

func (i BigInt) AddRaw(i2 int64) BigInt

AddRaw adds int64 to Int

func (BigInt) BigInt

func (i BigInt) BigInt() *big.Int

BigInt converts Int to big.Int

func (BigInt) Equal

func (i BigInt) Equal(i2 BigInt) bool

Equal compares two Ints

func (BigInt) GT

func (i BigInt) GT(i2 BigInt) bool

GT returns true if first Int is greater than second

func (BigInt) GTE

func (i BigInt) GTE(i2 BigInt) bool

GTE returns true if receiver Int is greater than or equal to the parameter Int.

func (BigInt) Int64

func (i BigInt) Int64() int64

Int64 converts Int to int64 Panics if the value is out of range

func (BigInt) IsInt64

func (i BigInt) IsInt64() bool

IsInt64 returns true if Int64() not panics

func (BigInt) IsNegative

func (i BigInt) IsNegative() bool

IsNegative returns true if Int is negative

func (BigInt) IsNil

func (i BigInt) IsNil() bool

IsNil returns true if Int is uninitialized

func (BigInt) IsPositive

func (i BigInt) IsPositive() bool

IsPositive returns true if Int is positive

func (BigInt) IsUint64

func (i BigInt) IsUint64() bool

IsUint64 returns true if Uint64() not panics

func (BigInt) IsZero

func (i BigInt) IsZero() bool

IsZero returns true if Int is zero

func (BigInt) LT

func (i BigInt) LT(i2 BigInt) bool

LT returns true if first Int is lesser than second

func (BigInt) LTE

func (i BigInt) LTE(i2 BigInt) bool

LTE returns true if first Int is less than or equal to second

func (BigInt) Marshal

func (i BigInt) Marshal() ([]byte, error)

Marshal implements the gogo proto custom type interface.

func (BigInt) MarshalAmino

func (i BigInt) MarshalAmino() ([]byte, error)

Override Amino binary serialization by proxying to protobuf.

func (BigInt) MarshalJSON

func (i BigInt) MarshalJSON() ([]byte, error)

MarshalJSON defines custom encoding scheme

func (*BigInt) MarshalTo

func (i *BigInt) MarshalTo(data []byte) (n int, err error)

MarshalTo implements the gogo proto custom type interface.

func (BigInt) MarshalYAML

func (i BigInt) MarshalYAML() (interface{}, error)

MarshalYAML returns the YAML representation.

func (BigInt) Mod

func (i BigInt) Mod(i2 BigInt) BigInt

Mod returns remainder after dividing with Int

func (BigInt) ModRaw

func (i BigInt) ModRaw(i2 int64) BigInt

ModRaw returns remainder after dividing with int64

func (BigInt) Mul

func (i BigInt) Mul(i2 BigInt) (res BigInt)

Mul multiples two Ints

func (BigInt) MulRaw

func (i BigInt) MulRaw(i2 int64) BigInt

MulRaw multiplies Int and int64

func (BigInt) Neg

func (i BigInt) Neg() (res BigInt)

Neg negates Int

func (BigInt) Quo

func (i BigInt) Quo(i2 BigInt) (res BigInt)

Quo divides Int with Int

func (BigInt) QuoRaw

func (i BigInt) QuoRaw(i2 int64) BigInt

QuoRaw divides Int with int64

func (BigInt) Sign

func (i BigInt) Sign() int

Sign returns sign of Int

func (*BigInt) Size

func (i *BigInt) Size() int

Size implements the gogo proto custom type interface.

func (BigInt) String

func (i BigInt) String() string

Human readable string

func (BigInt) Sub

func (i BigInt) Sub(i2 BigInt) (res BigInt)

Sub subtracts Int from another

func (BigInt) SubRaw

func (i BigInt) SubRaw(i2 int64) BigInt

SubRaw subtracts int64 from Int

func (BigInt) ToDec

func (i BigInt) ToDec() BigDec

ToDec converts Int to Dec

func (BigInt) Uint64

func (i BigInt) Uint64() uint64

Uint64 converts Int to uint64 Panics if the value is out of range

func (*BigInt) Unmarshal

func (i *BigInt) Unmarshal(data []byte) error

Unmarshal implements the gogo proto custom type interface.

func (*BigInt) UnmarshalAmino

func (i *BigInt) UnmarshalAmino(bz []byte) error

func (*BigInt) UnmarshalJSON

func (i *BigInt) UnmarshalJSON(bz []byte) error

UnmarshalJSON defines custom decoding scheme

type Dec

type Dec = sdkmath.LegacyDec

func AbsDifferenceWithSign

func AbsDifferenceWithSign(a, b Dec) (Dec, bool)

AbsDifferenceWithSign returns | a - b |, (a - b).sign() a is mutated and returned.

func GetPowPrecision

func GetPowPrecision() Dec

Returns the internal "power precision". All fractional exponentiation in osmosis is expected to be accurate up to powPrecision. *technically* the error term can be greater than this powPrecision, but for small bases this bound applies. See comments in the PowApprox function for more detail.

func MonotonicSqrt

func MonotonicSqrt(d Dec) (Dec, error)

Returns square root of d returns an error if one of the following conditions is met: - d is negative - d is too small to have a representable square root. This function guarantees: the returned root r, will be such that r^2 >= d This function is monotonic, i.e. if d1 >= d2, then sqrt(d1) >= sqrt(d2)

func MonotonicSqrtMut

func MonotonicSqrtMut(d Dec) (Dec, error)

func MustMonotonicSqrt

func MustMonotonicSqrt(d Dec) Dec

MustMonotonicSqrt returns the output of MonotonicSqrt, panicking on error.

func Pow

func Pow(base Dec, exp Dec) Dec

Pow computes base^(exp) However since the exponent is not an integer, we must do an approximation algorithm. TODO: In the future, lets add some optimized routines for common exponents, e.g. for common wIn / wOut ratios Many simple exponents like 2:1 pools.

func PowApprox

func PowApprox(originalBase Dec, exp Dec, precision Dec) Dec

Contract: 0 < base <= 2 0 <= exp < 1.

func SigFigRound

func SigFigRound(d Dec, tenToSigFig Int) Dec

SigFigRound rounds to a specified significant figure.

type ErrTolerance

type ErrTolerance struct {
	AdditiveTolerance       Dec
	MultiplicativeTolerance Dec
	RoundingDir             RoundingDirection
}

ErrTolerance is used to define a compare function, which checks if two ints are within a certain error tolerance of one another, and (optionally) that they are rounding in the correct direction. ErrTolerance.Compare(a, b) returns true iff: * RoundingMode = RoundUp, then a <= b * RoundingMode = RoundDown, then a >= b * |a - b| <= AdditiveTolerance * |a - b| / min(a, b) <= MultiplicativeTolerance

Each check is respectively ignored if the entry is nil. So AdditiveTolerance = Int{} or ZeroInt() MultiplicativeTolerance = Dec{} RoundingDir = RoundUnconstrained. Note that if AdditiveTolerance == 0, then this is equivalent to a standard compare.

func (ErrTolerance) Compare

func (e ErrTolerance) Compare(expected Int, actual Int) int

Compare returns if actual is within errTolerance of expected. returns 0 if it is returns 1 if not, and expected > actual. returns -1 if not, and expected < actual

func (ErrTolerance) CompareBigDec

func (e ErrTolerance) CompareBigDec(expected BigDec, actual BigDec) int

CompareBigDec validates if actual is within errTolerance of expected. returns 0 if it is returns 1 if not, and expected > actual. returns -1 if not, and expected < actual

func (ErrTolerance) CompareDec

func (e ErrTolerance) CompareDec(expected Dec, actual Dec) int

CompareDec validates if actual is within errTolerance of expected. returns 0 if it is returns 1 if not, and expected > actual. returns -1 if not, and expected < actual

func (ErrTolerance) EqualCoins

func (e ErrTolerance) EqualCoins(expectedCoins sdk.Coins, actualCoins sdk.Coins) bool

EqualCoins returns true iff the two coins are equal within the ErrTolerance constraints and false otherwise. TODO: move error tolerance functions to a separate file.

type Int

type Int = sdkmath.Int

func BinarySearch

func BinarySearch(f func(Int) (Int, error),
	lowerbound Int,
	upperbound Int,
	targetOutput Int,
	errTolerance ErrTolerance,
	maxIterations int,
) (Int, error)

Binary search inputs between [lowerbound, upperbound] to a monotonic increasing function f. We stop once f(found_input) meets the ErrTolerance constraints. If we perform more than maxIterations (or equivalently lowerbound = upperbound), we return an error.

type RoundingDirection

type RoundingDirection int
const (
	RoundUnconstrained RoundingDirection = 0
	RoundUp            RoundingDirection = 1
	RoundDown          RoundingDirection = 2
	RoundBankers       RoundingDirection = 3
)

type SdkDec

type SdkDec[D any] interface {
	Add(SdkDec[D]) SdkDec[D]
	Quo(SdkDec[D]) SdkDec[D]
	QuoRaw(int64) SdkDec[D]
}

SdkDec

type Stringer

type Stringer interface {
	String() string
}

type Uint

type Uint = sdkmath.Uint

Jump to

Keyboard shortcuts

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