Documentation ¶
Index ¶
- Constants
- Variables
- func FormatPrecDec(v string) (string, error)
- func PrecDecApproxEq(t *testing.T, d1, d2, tol PrecDec) (*testing.T, bool, string, string, string)
- func PrecDecEq(t *testing.T, exp, got PrecDec) (*testing.T, bool, string, string, string)
- func PrecDecsEqual(d1s, d2s []PrecDec) bool
- func SortablePrecDecBytes(dec PrecDec) []byte
- func ValidSortablePrecDec(dec PrecDec) bool
- type PrecDec
- func MaxPrecDec(d1, d2 PrecDec) PrecDec
- func MinPrecDec(d1, d2 PrecDec) PrecDec
- func MustNewPrecDecFromStr(s string) PrecDec
- func NewPrecDec(i int64) PrecDec
- func NewPrecDecFromBigInt(i *big.Int) PrecDec
- func NewPrecDecFromBigIntWithPrec(i *big.Int, prec int64) PrecDec
- func NewPrecDecFromInt(i math.Int) PrecDec
- func NewPrecDecFromIntWithPrec(i math.Int, prec int64) PrecDec
- func NewPrecDecFromStr(str string) (PrecDec, error)
- func NewPrecDecWithPrec(i, prec int64) PrecDec
- func OnePrecDec() PrecDec
- func SmallestPrecDec() PrecDec
- func ZeroPrecDec() PrecDec
- func (d PrecDec) Abs() PrecDec
- func (d PrecDec) AbsMut() PrecDec
- func (d PrecDec) Add(d2 PrecDec) PrecDec
- func (d PrecDec) AddMut(d2 PrecDec) PrecDec
- func (d PrecDec) ApproxRoot(root uint64) (guess PrecDec, err error)
- func (d PrecDec) ApproxSqrt() (PrecDec, error)
- func (d PrecDec) BigInt() *big.Int
- func (d PrecDec) Ceil() PrecDec
- func (d PrecDec) Clone() PrecDec
- func (d PrecDec) Equal(d2 PrecDec) bool
- func (d PrecDec) Float64() (float64, error)
- func (d PrecDec) Format(s fmt.State)
- func (d PrecDec) GT(d2 PrecDec) bool
- func (d PrecDec) GTE(d2 PrecDec) bool
- func (d PrecDec) ImmutOp(op func(PrecDec, PrecDec) PrecDec, d2 PrecDec) PrecDec
- func (d PrecDec) ImmutOpInt(op func(PrecDec, math.Int) PrecDec, d2 math.Int) PrecDec
- func (d PrecDec) ImmutOpInt64(op func(PrecDec, int64) PrecDec, d2 int64) PrecDec
- func (d PrecDec) IsInteger() bool
- func (d PrecDec) IsNegative() bool
- func (d PrecDec) IsNil() bool
- func (d PrecDec) IsPositive() bool
- func (d PrecDec) IsZero() bool
- func (d PrecDec) LT(d2 PrecDec) bool
- func (d PrecDec) LTE(d2 PrecDec) bool
- func (d PrecDec) Marshal() ([]byte, error)
- func (d PrecDec) MarshalAmino() ([]byte, error)
- func (d PrecDec) MarshalJSON() ([]byte, error)
- func (d *PrecDec) MarshalTo(data []byte) (n int, err error)
- func (d PrecDec) MarshalYAML() (interface{}, error)
- func (d PrecDec) Mul(d2 PrecDec) PrecDec
- func (d PrecDec) MulInt(i math.Int) PrecDec
- func (d PrecDec) MulInt64(i int64) PrecDec
- func (d PrecDec) MulInt64Mut(i int64) PrecDec
- func (d PrecDec) MulIntMut(i math.Int) PrecDec
- func (d PrecDec) MulMut(d2 PrecDec) PrecDec
- func (d PrecDec) MulTruncate(d2 PrecDec) PrecDec
- func (d PrecDec) MulTruncateMut(d2 PrecDec) PrecDec
- func (d PrecDec) MustFloat64() float64
- func (d PrecDec) Neg() PrecDec
- func (d PrecDec) NegMut() PrecDec
- func (d PrecDec) Power(power uint64) PrecDec
- func (d PrecDec) PowerMut(power uint64) PrecDec
- func (d PrecDec) Quo(d2 PrecDec) PrecDec
- func (d PrecDec) QuoInt(i math.Int) PrecDec
- func (d PrecDec) QuoInt64(i int64) PrecDec
- func (d PrecDec) QuoInt64Mut(i int64) PrecDec
- func (d PrecDec) QuoIntMut(i math.Int) PrecDec
- func (d PrecDec) QuoMut(d2 PrecDec) PrecDec
- func (d PrecDec) QuoRoundUp(d2 PrecDec) PrecDec
- func (d PrecDec) QuoRoundupMut(d2 PrecDec) PrecDec
- func (d PrecDec) QuoTruncate(d2 PrecDec) PrecDec
- func (d PrecDec) QuoTruncateMut(d2 PrecDec) PrecDec
- func (d PrecDec) RoundInt() math.Int
- func (d PrecDec) RoundInt64() int64
- func (d PrecDec) Set(d2 PrecDec) PrecDec
- func (d PrecDec) SetInt64(i int64) PrecDec
- func (d *PrecDec) Size() int
- func (d PrecDec) String() string
- func (d PrecDec) Sub(d2 PrecDec) PrecDec
- func (d PrecDec) SubMut(d2 PrecDec) PrecDec
- func (d PrecDec) TruncateInt() math.Int
- func (d PrecDec) TruncateInt64() int64
- func (d PrecDec) TruncatePrecDec() PrecDec
- func (d *PrecDec) Unmarshal(data []byte) error
- func (d *PrecDec) UnmarshalAmino(bz []byte) error
- func (d *PrecDec) UnmarshalJSON(bz []byte) error
Constants ¶
const ( // number of decimal places Precision = 27 // bits required to represent the above precision // Ceiling[Log2[10^Precision - 1]] PrecDecimalPrecisionBits = 90 MaxBitLen = 256 )
Variables ¶
var ( ErrEmptyPrecDecimalStr = errors.New("decimal string cannot be empty") ErrInvalidPrecDecimalLength = errors.New("invalid decimal length") ErrInvalidPrecDecimalStr = errors.New("invalid decimal string") )
PrecDecimal errors
Functions ¶
func FormatPrecDec ¶
FormatPrecDec formats a decimal (as encoded in protobuf) into a value-rendered string following ADR-050. This function operates with string manipulation (instead of manipulating the sdk.PrecDec object).
func PrecDecApproxEq ¶
func PrecDecsEqual ¶
test if two decimal arrays are equal
func SortablePrecDecBytes ¶
SortablePrecDecBytes returns a byte slice representation of a PrecDec that can be sorted. Left and right pads with 0s so there are 18 digits to left and right of the decimal point. For this reason, there is a maximum and minimum value for this, enforced by ValidSortablePrecDec.
func ValidSortablePrecDec ¶
ValidSortablePrecDec ensures that a PrecDec is within the sortable bounds, a PrecDec can't have a precision of less than 10^-18. Max sortable decimal was set to the reciprocal of SmallestPrecDec.
Types ¶
type PrecDec ¶
type PrecDec struct {
// contains filtered or unexported fields
}
NOTE: never use new(Dec) or else we will panic unmarshalling into the nil embedded big.Int
var MaxSortablePrecDec PrecDec
MaxSortablePrecDec is the largest PrecDec that can be passed into SortablePrecDecBytes() Its negative form is the least PrecDec that can be passed in.
func MustNewPrecDecFromStr ¶
PrecDecimal from string, panic on error
func NewPrecDec ¶
create a new PrecDec from integer assuming whole number
func NewPrecDecFromBigInt ¶
create a new PrecDec from big integer assuming whole numbers CONTRACT: prec <= Precision
func NewPrecDecFromBigIntWithPrec ¶
create a new PrecDec from big integer assuming whole numbers CONTRACT: prec <= Precision
func NewPrecDecFromInt ¶
create a new PrecDec from big integer assuming whole numbers CONTRACT: prec <= Precision
func NewPrecDecFromIntWithPrec ¶
create a new PrecDec from big integer with decimal place at prec CONTRACT: prec <= Precision
func NewPrecDecFromStr ¶
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 NewPrecDecWithPrec ¶
create a new PrecDec from integer with decimal place at prec CONTRACT: prec <= Precision
func OnePrecDec ¶
func OnePrecDec() PrecDec
func SmallestPrecDec ¶
func SmallestPrecDec() PrecDec
func ZeroPrecDec ¶
func ZeroPrecDec() PrecDec
func (PrecDec) ApproxRoot ¶
ApproxRoot returns an approximate estimation of a PrecDec'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 (PrecDec) ApproxSqrt ¶
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.
func (PrecDec) Ceil ¶
Ceil returns the smallest interger value (as a decimal) that is greater than or equal to the given decimal.
func (PrecDec) Float64 ¶
Float64 returns the float64 representation of a PrecDec. Will return the error if the conversion failed.
func (PrecDec) ImmutOpInt ¶
func (PrecDec) ImmutOpInt64 ¶
func (PrecDec) IsNegative ¶
func (PrecDec) IsPositive ¶
func (PrecDec) MarshalAmino ¶
Override Amino binary serialization by proxying to protobuf.
func (PrecDec) MarshalJSON ¶
MarshalJSON marshals the decimal
func (PrecDec) MarshalYAML ¶
MarshalYAML returns the YAML representation.
func (PrecDec) MulInt64Mut ¶
func (PrecDec) MulTruncate ¶
multiplication truncate
func (PrecDec) MulTruncateMut ¶
mutable multiplication truncage
func (PrecDec) MustFloat64 ¶
MustFloat64 returns the float64 representation of a PrecDec. Would panic if the conversion failed.
func (PrecDec) QuoInt64Mut ¶
func (PrecDec) QuoRoundupMut ¶
mutable quotient, round up
func (PrecDec) QuoTruncateMut ¶
mutable quotient truncate
func (PrecDec) RoundInt64 ¶
RoundInt64 rounds the decimal using bankers rounding
func (PrecDec) TruncateInt ¶
TruncateInt truncates the decimals from the number and returns an Int
func (PrecDec) TruncateInt64 ¶
TruncateInt64 truncates the decimals from the number and returns an int64
func (PrecDec) TruncatePrecDec ¶
TruncatePrecDec truncates the decimals from the number and returns a PrecDec
func (*PrecDec) UnmarshalAmino ¶
func (*PrecDec) UnmarshalJSON ¶
UnmarshalJSON defines custom decoding scheme