math

package
v4.0.0-rc0 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	// number of decimal places
	Precision = 27

	// bits required to represent the above precision
	// Ceiling[Log2[10^Precision - 1]]
	PrecDecimalPrecisionBits = 90

	MaxBitLen = 256
)

Variables

View Source
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

func FormatPrecDec(v string) (string, error)

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 PrecDecApproxEq(t *testing.T, d1, d2, tol PrecDec) (*testing.T, bool, string, string, string)

func PrecDecEq

func PrecDecEq(t *testing.T, exp, got PrecDec) (*testing.T, bool, string, string, string)

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

func PrecDecsEqual

func PrecDecsEqual(d1s, d2s []PrecDec) bool

test if two decimal arrays are equal

func SortablePrecDecBytes

func SortablePrecDecBytes(dec PrecDec) []byte

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

func ValidSortablePrecDec(dec PrecDec) bool

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 MaxPrecDec

func MaxPrecDec(d1, d2 PrecDec) PrecDec

maximum decimal between two

func MinPrecDec

func MinPrecDec(d1, d2 PrecDec) PrecDec

minimum decimal between two

func MustNewPrecDecFromStr

func MustNewPrecDecFromStr(s string) PrecDec

PrecDecimal from string, panic on error

func NewPrecDec

func NewPrecDec(i int64) PrecDec

create a new PrecDec from integer assuming whole number

func NewPrecDecFromBigInt

func NewPrecDecFromBigInt(i *big.Int) PrecDec

create a new PrecDec from big integer assuming whole numbers CONTRACT: prec <= Precision

func NewPrecDecFromBigIntWithPrec

func NewPrecDecFromBigIntWithPrec(i *big.Int, prec int64) PrecDec

create a new PrecDec from big integer assuming whole numbers CONTRACT: prec <= Precision

func NewPrecDecFromInt

func NewPrecDecFromInt(i math.Int) PrecDec

create a new PrecDec from big integer assuming whole numbers CONTRACT: prec <= Precision

func NewPrecDecFromIntWithPrec

func NewPrecDecFromIntWithPrec(i math.Int, prec int64) PrecDec

create a new PrecDec from big integer with decimal place at prec CONTRACT: prec <= Precision

func NewPrecDecFromStr

func NewPrecDecFromStr(str string) (PrecDec, 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 NewPrecDecWithPrec

func NewPrecDecWithPrec(i, prec int64) PrecDec

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

func (d PrecDec) Abs() PrecDec

func (PrecDec) AbsMut

func (d PrecDec) AbsMut() PrecDec

func (PrecDec) Add

func (d PrecDec) Add(d2 PrecDec) PrecDec

addition

func (PrecDec) AddMut

func (d PrecDec) AddMut(d2 PrecDec) PrecDec

mutable addition

func (PrecDec) ApproxRoot

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

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

func (d PrecDec) ApproxSqrt() (PrecDec, 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.

func (PrecDec) BigInt

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

BigInt returns a copy of the underlying big.Int.

func (PrecDec) Ceil

func (d PrecDec) Ceil() PrecDec

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

func (PrecDec) Clone

func (d PrecDec) Clone() PrecDec

func (PrecDec) Equal

func (d PrecDec) Equal(d2 PrecDec) bool

func (PrecDec) Float64

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

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

func (PrecDec) Format

func (d PrecDec) Format(s fmt.State)

Format decimal state

func (PrecDec) GT

func (d PrecDec) GT(d2 PrecDec) bool

func (PrecDec) GTE

func (d PrecDec) GTE(d2 PrecDec) bool

func (PrecDec) ImmutOp

func (d PrecDec) ImmutOp(op func(PrecDec, PrecDec) PrecDec, d2 PrecDec) PrecDec

func (PrecDec) ImmutOpInt

func (d PrecDec) ImmutOpInt(op func(PrecDec, math.Int) PrecDec, d2 math.Int) PrecDec

func (PrecDec) ImmutOpInt64

func (d PrecDec) ImmutOpInt64(op func(PrecDec, int64) PrecDec, d2 int64) PrecDec

func (PrecDec) IsInteger

func (d PrecDec) IsInteger() bool

is integer, e.g. decimals are zero

func (PrecDec) IsNegative

func (d PrecDec) IsNegative() bool

func (PrecDec) IsNil

func (d PrecDec) IsNil() bool

func (PrecDec) IsPositive

func (d PrecDec) IsPositive() bool

func (PrecDec) IsZero

func (d PrecDec) IsZero() bool

func (PrecDec) LT

func (d PrecDec) LT(d2 PrecDec) bool

func (PrecDec) LTE

func (d PrecDec) LTE(d2 PrecDec) bool

func (PrecDec) Marshal

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

Marshal implements the gogo proto custom type interface.

func (PrecDec) MarshalAmino

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

Override Amino binary serialization by proxying to protobuf.

func (PrecDec) MarshalJSON

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

MarshalJSON marshals the decimal

func (*PrecDec) MarshalTo

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

MarshalTo implements the gogo proto custom type interface.

func (PrecDec) MarshalYAML

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

MarshalYAML returns the YAML representation.

func (PrecDec) Mul

func (d PrecDec) Mul(d2 PrecDec) PrecDec

multiplication

func (PrecDec) MulInt

func (d PrecDec) MulInt(i math.Int) PrecDec

multiplication

func (PrecDec) MulInt64

func (d PrecDec) MulInt64(i int64) PrecDec

MulInt64 - multiplication with int64

func (PrecDec) MulInt64Mut

func (d PrecDec) MulInt64Mut(i int64) PrecDec

func (PrecDec) MulIntMut

func (d PrecDec) MulIntMut(i math.Int) PrecDec

func (PrecDec) MulMut

func (d PrecDec) MulMut(d2 PrecDec) PrecDec

mutable multiplication

func (PrecDec) MulTruncate

func (d PrecDec) MulTruncate(d2 PrecDec) PrecDec

multiplication truncate

func (PrecDec) MulTruncateMut

func (d PrecDec) MulTruncateMut(d2 PrecDec) PrecDec

mutable multiplication truncage

func (PrecDec) MustFloat64

func (d PrecDec) MustFloat64() float64

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

func (PrecDec) Neg

func (d PrecDec) Neg() PrecDec

func (PrecDec) NegMut

func (d PrecDec) NegMut() PrecDec

func (PrecDec) Power

func (d PrecDec) Power(power uint64) PrecDec

Power returns a the result of raising to a positive integer power

func (PrecDec) PowerMut

func (d PrecDec) PowerMut(power uint64) PrecDec

func (PrecDec) Quo

func (d PrecDec) Quo(d2 PrecDec) PrecDec

quotient

func (PrecDec) QuoInt

func (d PrecDec) QuoInt(i math.Int) PrecDec

quotient

func (PrecDec) QuoInt64

func (d PrecDec) QuoInt64(i int64) PrecDec

QuoInt64 - quotient with int64

func (PrecDec) QuoInt64Mut

func (d PrecDec) QuoInt64Mut(i int64) PrecDec

func (PrecDec) QuoIntMut

func (d PrecDec) QuoIntMut(i math.Int) PrecDec

func (PrecDec) QuoMut

func (d PrecDec) QuoMut(d2 PrecDec) PrecDec

mutable quotient

func (PrecDec) QuoRoundUp

func (d PrecDec) QuoRoundUp(d2 PrecDec) PrecDec

quotient, round up

func (PrecDec) QuoRoundupMut

func (d PrecDec) QuoRoundupMut(d2 PrecDec) PrecDec

mutable quotient, round up

func (PrecDec) QuoTruncate

func (d PrecDec) QuoTruncate(d2 PrecDec) PrecDec

quotient truncate

func (PrecDec) QuoTruncateMut

func (d PrecDec) QuoTruncateMut(d2 PrecDec) PrecDec

mutable quotient truncate

func (PrecDec) RoundInt

func (d PrecDec) RoundInt() math.Int

RoundInt round the decimal using bankers rounding

func (PrecDec) RoundInt64

func (d PrecDec) RoundInt64() int64

RoundInt64 rounds the decimal using bankers rounding

func (PrecDec) Set

func (d PrecDec) Set(d2 PrecDec) PrecDec

func (PrecDec) SetInt64

func (d PrecDec) SetInt64(i int64) PrecDec

func (*PrecDec) Size

func (d *PrecDec) Size() int

Size implements the gogo proto custom type interface.

func (PrecDec) String

func (d PrecDec) String() string

func (PrecDec) Sub

func (d PrecDec) Sub(d2 PrecDec) PrecDec

subtraction

func (PrecDec) SubMut

func (d PrecDec) SubMut(d2 PrecDec) PrecDec

mutable subtraction

func (PrecDec) TruncateInt

func (d PrecDec) TruncateInt() math.Int

TruncateInt truncates the decimals from the number and returns an Int

func (PrecDec) TruncateInt64

func (d PrecDec) TruncateInt64() int64

TruncateInt64 truncates the decimals from the number and returns an int64

func (PrecDec) TruncatePrecDec

func (d PrecDec) TruncatePrecDec() PrecDec

TruncatePrecDec truncates the decimals from the number and returns a PrecDec

func (*PrecDec) Unmarshal

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

Unmarshal implements the gogo proto custom type interface.

func (*PrecDec) UnmarshalAmino

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

func (*PrecDec) UnmarshalJSON

func (d *PrecDec) 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