Documentation ¶
Index ¶
- Constants
- Variables
- func DecEq(t *testing.T, exp, got Dec) (*testing.T, bool, string, string, string)
- func DecsEqual(d1s, d2s []Dec) bool
- func SortableDecBytes(dec Dec) []byte
- func ValidSortableDec(dec Dec) bool
- type CustomProtobufType
- type Dec
- func MaxDec(d1, d2 Dec) Dec
- func MinDec(d1, d2 Dec) Dec
- func MustNewDecFromStr(s string) Dec
- func NewDec(i int64) Dec
- func NewDecFromBigInt(i *big.Int) Dec
- func NewDecFromBigIntWithPrec(i *big.Int, prec int64) Dec
- func NewDecFromInt(i sdk.Int) Dec
- func NewDecFromIntWithPrec(i sdk.Int, prec int64) Dec
- func NewDecFromStr(str string) (Dec, error)
- func NewDecWithPrec(i, prec int64) Dec
- func OneDec() Dec
- func SmallestDec() Dec
- func ZeroDec() Dec
- func (d Dec) Abs() Dec
- func (d Dec) Add(d2 Dec) Dec
- func (d Dec) ApproxRoot(root uint64) (guess Dec, err error)
- func (d Dec) ApproxSqrt() (Dec, error)
- func (d Dec) BigInt() *big.Int
- func (d Dec) Ceil() Dec
- func (d Dec) Equal(d2 Dec) bool
- func (d Dec) Float64() (float64, error)
- func (d Dec) Format(s fmt.State, verb rune)
- func (d Dec) GT(d2 Dec) bool
- func (d Dec) GTE(d2 Dec) bool
- func (d Dec) IsInteger() bool
- func (d Dec) IsNegative() bool
- func (d Dec) IsNil() bool
- func (d Dec) IsPositive() bool
- func (d Dec) IsZero() bool
- func (d Dec) LT(d2 Dec) bool
- func (d Dec) LTE(d2 Dec) bool
- func (d Dec) Marshal() ([]byte, error)
- func (d Dec) MarshalAmino() ([]byte, error)
- func (d Dec) MarshalJSON() ([]byte, error)
- func (d *Dec) MarshalTo(data []byte) (n int, err error)
- func (d Dec) MarshalYAML() (interface{}, error)
- func (d Dec) Mul(d2 Dec) Dec
- func (d Dec) MulInt(i sdk.Int) Dec
- func (d Dec) MulInt64(i int64) Dec
- func (d Dec) MulTruncate(d2 Dec) Dec
- func (d Dec) MustFloat64() float64
- func (d Dec) Neg() Dec
- func (d Dec) Power(power uint64) Dec
- func (d Dec) Quo(d2 Dec) Dec
- func (d Dec) QuoInt(i sdk.Int) Dec
- func (d Dec) QuoInt64(i int64) Dec
- func (d Dec) QuoRoundUp(d2 Dec) Dec
- func (d Dec) QuoTruncate(d2 Dec) Dec
- func (d Dec) RoundInt() sdk.Int
- func (d Dec) RoundInt64() int64
- func (d *Dec) Size() int
- func (d Dec) String() string
- func (d Dec) Sub(d2 Dec) Dec
- func (d Dec) TruncateDec() Dec
- func (d Dec) TruncateInt() sdk.Int
- func (d Dec) TruncateInt64() int64
- func (d *Dec) Unmarshal(data []byte) error
- func (d *Dec) UnmarshalAmino(bz []byte) error
- func (d *Dec) UnmarshalJSON(bz []byte) error
Constants ¶
const ( // number of decimal places Precision = 6 // bytes required to represent the above precision // Ceiling[Log2[999 999 999 999 999 999]] DecimalPrecisionBits = 60 )
const (
FisDenom = "ufis"
)
Variables ¶
var ( ErrEmptyDecimalStr = errors.New("decimal string cannot be empty") ErrInvalidDecimalLength = errors.New("invalid decimal length") ErrInvalidDecimalStr = errors.New("invalid decimal string") )
Decimal errors
var MaxSortableDec = OneDec().Quo(SmallestDec())
MaxSortableDec is the largest Dec that can be passed into SortableDecBytes() Its negative form is the least Dec that can be passed in.
Functions ¶
func SortableDecBytes ¶
SortableDecBytes returns a byte slice representation of a Dec 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 ValidSortableDec.
func ValidSortableDec ¶
ValidSortableDec ensures that a Dec is within the sortable bounds, a Dec can't have a precision of less than 10^-18. Max sortable decimal was set to the reciprocal of SmallestDec.
Types ¶
type CustomProtobufType ¶
type CustomProtobufType interface { Marshal() ([]byte, error) MarshalTo(data []byte) (n int, err error) Unmarshal(data []byte) error Size() int MarshalJSON() ([]byte, error) UnmarshalJSON(data []byte) error }
CustomProtobufType defines the interface custom gogo proto types must implement in order to be used as a "customtype" extension.
ref: https://github.com/gogo/protobuf/blob/master/custom_types.md
type Dec ¶
type Dec struct {
// contains filtered or unexported fields
}
NOTE: never use new(Dec) or else we will panic unmarshalling into the nil embedded big.Int
func NewDecFromBigInt ¶
create a new Dec from big integer assuming whole numbers CONTRACT: prec <= Precision
func NewDecFromBigIntWithPrec ¶
create a new Dec from big integer assuming whole numbers CONTRACT: prec <= Precision
func NewDecFromInt ¶
create a new Dec from big integer assuming whole numbers CONTRACT: prec <= Precision
func NewDecFromIntWithPrec ¶
create a new Dec from big integer with decimal place at prec CONTRACT: prec <= Precision
func NewDecFromStr ¶
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 NewDecWithPrec ¶
create a new Dec from integer with decimal place at prec CONTRACT: prec <= Precision
func SmallestDec ¶
func SmallestDec() Dec
func (Dec) ApproxRoot ¶
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 (Dec) 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 (Dec) Ceil ¶
Ceil returns the smallest interger value (as a decimal) that is greater than or equal to the given decimal.
func (Dec) Float64 ¶
Float64 returns the float64 representation of a Dec. Will return the error if the conversion failed.
func (Dec) IsNegative ¶
func (Dec) IsPositive ¶
func (Dec) MarshalAmino ¶
Override Amino binary serialization by proxying to protobuf.
func (Dec) MarshalJSON ¶
MarshalJSON marshals the decimal
func (Dec) MarshalYAML ¶
MarshalYAML returns the YAML representation.
func (Dec) MustFloat64 ¶
MustFloat64 returns the float64 representation of a Dec. Would panic if the conversion failed.
func (Dec) RoundInt64 ¶
RoundInt64 rounds the decimal using bankers rounding
func (Dec) TruncateDec ¶
TruncateDec truncates the decimals from the number and returns a Dec
func (Dec) TruncateInt ¶
TruncateInt truncates the decimals from the number and returns an Int
func (Dec) TruncateInt64 ¶
TruncateInt64 truncates the decimals from the number and returns an int64
func (*Dec) UnmarshalAmino ¶
func (*Dec) UnmarshalJSON ¶
UnmarshalJSON defines custom decoding scheme