math

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2023 License: Apache-2.0 Imports: 8 Imported by: 5

Documentation

Overview

Package math implements custom Cosmos SDK math types used for arithmetic operations. Signed and unsigned integer types utilize Golang's standard library big integers types, having a maximum bit length of 256 bits.

Index

Constants

View Source
const (
	// number of decimal places
	LegacyPrecision = 18

	// bits required to represent the above precision
	// Ceiling[Log2[10^Precision - 1]]
	LegacyDecimalPrecisionBits = 60
)
View Source
const MaxBitLen = 256

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

Variables

View Source
var (
	ErrLegacyEmptyDecimalStr      = errors.New("decimal string cannot be empty")
	ErrLegacyInvalidDecimalLength = errors.New("invalid decimal length")
	ErrLegacyInvalidDecimalStr    = errors.New("invalid decimal string")
)

Decimal errors

Functions

func FormatDec

func FormatDec(v string) (string, error)

FormatDec 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.Dec object).

func FormatInt

func FormatInt(v string) (string, error)

FormatInt formats an integer (encoded as in protobuf) into a value-rendered string following ADR-050. This function operates with string manipulation (instead of manipulating the int or sdk.Int object).

func IntEq

func IntEq(t *testing.T, exp, got Int) (*testing.T, bool, string, string, string)

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

func LegacyDecApproxEq

func LegacyDecApproxEq(t *testing.T, d1 LegacyDec, d2 LegacyDec, tol LegacyDec) (*testing.T, bool, string, string, string)

func LegacyDecEq

func LegacyDecEq(t *testing.T, exp, got LegacyDec) (*testing.T, bool, string, string, string)

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

func LegacyDecsEqual

func LegacyDecsEqual(d1s, d2s []LegacyDec) bool

test if two decimal arrays are equal

func LegacySortableDecBytes

func LegacySortableDecBytes(dec LegacyDec) []byte

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 LegacyValidSortableDec

func LegacyValidSortableDec(dec LegacyDec) bool

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.

func UintOverflow

func UintOverflow(i *big.Int) error

UintOverflow returns true if a given unsigned integer overflows and false otherwise.

Types

type Int

type Int 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 MaxInt

func MaxInt(i, i2 Int) Int

MaxInt returns the maximum between two integers.

func MinInt

func MinInt(i1, i2 Int) Int

return the minimum of the ints

func NewInt

func NewInt(n int64) Int

NewInt constructs Int from int64

func NewIntFromBigInt

func NewIntFromBigInt(i *big.Int) Int

NewIntFromBigInt 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 NewIntFromString

func NewIntFromString(s string) (res Int, ok bool)

NewIntFromString constructs Int from string

func NewIntFromUint64

func NewIntFromUint64(n uint64) Int

NewIntFromUint64 constructs an Int from a uint64.

func NewIntWithDecimal

func NewIntWithDecimal(n int64, dec int) Int

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

func OneInt

func OneInt() Int

OneInt returns Int value with one

func ZeroInt

func ZeroInt() Int

ZeroInt returns Int value with zero

func (Int) Abs

func (i Int) Abs() Int

Abs returns the absolute value of Int.

func (Int) Add

func (i Int) Add(i2 Int) (res Int)

Add adds Int from another

func (Int) AddRaw

func (i Int) AddRaw(i2 int64) Int

AddRaw adds int64 to Int

func (Int) BigInt

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

BigInt converts Int to big.Int

func (Int) Equal

func (i Int) Equal(i2 Int) bool

Equal compares two Ints

func (Int) GT

func (i Int) GT(i2 Int) bool

GT returns true if first Int is greater than second

func (Int) GTE

func (i Int) GTE(i2 Int) bool

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

func (Int) Int64

func (i Int) Int64() int64

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

func (Int) IsInt64

func (i Int) IsInt64() bool

IsInt64 returns true if Int64() not panics

func (Int) IsNegative

func (i Int) IsNegative() bool

IsNegative returns true if Int is negative

func (Int) IsNil

func (i Int) IsNil() bool

IsNil returns true if Int is uninitialized

func (Int) IsPositive

func (i Int) IsPositive() bool

IsPositive returns true if Int is positive

func (Int) IsUint64

func (i Int) IsUint64() bool

IsUint64 returns true if Uint64() not panics

func (Int) IsZero

func (i Int) IsZero() bool

IsZero returns true if Int is zero

func (Int) LT

func (i Int) LT(i2 Int) bool

LT returns true if first Int is lesser than second

func (Int) LTE

func (i Int) LTE(i2 Int) bool

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

func (Int) Marshal

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

Marshal implements the gogo proto custom type interface.

func (Int) MarshalAmino

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

Override Amino binary serialization by proxying to protobuf.

func (Int) MarshalJSON

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

MarshalJSON defines custom encoding scheme

func (*Int) MarshalTo

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

MarshalTo implements the gogo proto custom type interface.

func (Int) MarshalYAML

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

MarshalYAML returns the YAML representation.

func (Int) Mod

func (i Int) Mod(i2 Int) Int

Mod returns remainder after dividing with Int

func (Int) ModRaw

func (i Int) ModRaw(i2 int64) Int

ModRaw returns remainder after dividing with int64

func (Int) Mul

func (i Int) Mul(i2 Int) (res Int)

Mul multiples two Ints

func (Int) MulRaw

func (i Int) MulRaw(i2 int64) Int

MulRaw multipies Int and int64

func (Int) Neg

func (i Int) Neg() (res Int)

Neg negates Int

func (Int) Quo

func (i Int) Quo(i2 Int) (res Int)

Quo divides Int with Int

func (Int) QuoRaw

func (i Int) QuoRaw(i2 int64) Int

QuoRaw divides Int with int64

func (Int) Sign

func (i Int) Sign() int

Sign returns sign of Int

func (*Int) Size

func (i *Int) Size() int

Size implements the gogo proto custom type interface.

func (Int) String

func (i Int) String() string

Human readable string

func (Int) Sub

func (i Int) Sub(i2 Int) (res Int)

Sub subtracts Int from another

func (Int) SubRaw

func (i Int) SubRaw(i2 int64) Int

SubRaw subtracts int64 from Int

func (Int) Uint64

func (i Int) Uint64() uint64

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

func (*Int) Unmarshal

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

Unmarshal implements the gogo proto custom type interface.

func (*Int) UnmarshalAmino

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

func (*Int) UnmarshalJSON

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

UnmarshalJSON defines custom decoding scheme

type LegacyDec

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

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

var LegacyMaxSortableDec LegacyDec

MaxSortableDec is the largest Dec that can be passed into SortableDecBytes() Its negative form is the least Dec that can be passed in.

func LegacyMaxDec

func LegacyMaxDec(d1, d2 LegacyDec) LegacyDec

maximum decimal between two

func LegacyMinDec

func LegacyMinDec(d1, d2 LegacyDec) LegacyDec

minimum decimal between two

func LegacyMustNewDecFromStr

func LegacyMustNewDecFromStr(s string) LegacyDec

Decimal from string, panic on error

func LegacyNewDec

func LegacyNewDec(i int64) LegacyDec

create a new Dec from integer assuming whole number

func LegacyNewDecFromBigInt

func LegacyNewDecFromBigInt(i *big.Int) LegacyDec

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

func LegacyNewDecFromBigIntWithPrec

func LegacyNewDecFromBigIntWithPrec(i *big.Int, prec int64) LegacyDec

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

func LegacyNewDecFromInt

func LegacyNewDecFromInt(i Int) LegacyDec

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

func LegacyNewDecFromIntWithPrec

func LegacyNewDecFromIntWithPrec(i Int, prec int64) LegacyDec

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

func LegacyNewDecFromStr

func LegacyNewDecFromStr(str string) (LegacyDec, 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 LegacyNewDecWithPrec

func LegacyNewDecWithPrec(i, prec int64) LegacyDec

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

func LegacyOneDec

func LegacyOneDec() LegacyDec

func LegacySmallestDec

func LegacySmallestDec() LegacyDec

func LegacyZeroDec

func LegacyZeroDec() LegacyDec

func (LegacyDec) Abs

func (d LegacyDec) Abs() LegacyDec

func (LegacyDec) Add

func (d LegacyDec) Add(d2 LegacyDec) LegacyDec

addition

func (LegacyDec) AddMut

func (d LegacyDec) AddMut(d2 LegacyDec) LegacyDec

mutable addition

func (LegacyDec) ApproxRoot

func (d LegacyDec) ApproxRoot(root uint64) (guess LegacyDec, 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 (LegacyDec) ApproxSqrt

func (d LegacyDec) ApproxSqrt() (LegacyDec, 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 (LegacyDec) BigInt

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

BigInt returns a copy of the underlying big.Int.

func (LegacyDec) Ceil

func (d LegacyDec) Ceil() LegacyDec

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

func (LegacyDec) Clone

func (d LegacyDec) Clone() LegacyDec

func (LegacyDec) Equal

func (d LegacyDec) Equal(d2 LegacyDec) bool

func (LegacyDec) Float64

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

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

func (LegacyDec) Format

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

format decimal state

func (LegacyDec) GT

func (d LegacyDec) GT(d2 LegacyDec) bool

func (LegacyDec) GTE

func (d LegacyDec) GTE(d2 LegacyDec) bool

func (LegacyDec) ImmutOp

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

func (LegacyDec) ImmutOpInt

func (d LegacyDec) ImmutOpInt(op func(LegacyDec, Int) LegacyDec, d2 Int) LegacyDec

func (LegacyDec) ImmutOpInt64

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

func (LegacyDec) IsInteger

func (d LegacyDec) IsInteger() bool

is integer, e.g. decimals are zero

func (LegacyDec) IsNegative

func (d LegacyDec) IsNegative() bool

func (LegacyDec) IsNil

func (d LegacyDec) IsNil() bool

func (LegacyDec) IsPositive

func (d LegacyDec) IsPositive() bool

func (LegacyDec) IsZero

func (d LegacyDec) IsZero() bool

func (LegacyDec) LT

func (d LegacyDec) LT(d2 LegacyDec) bool

func (LegacyDec) LTE

func (d LegacyDec) LTE(d2 LegacyDec) bool

func (LegacyDec) Marshal

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

Marshal implements the gogo proto custom type interface.

func (LegacyDec) MarshalAmino

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

Override Amino binary serialization by proxying to protobuf.

func (LegacyDec) MarshalJSON

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

MarshalJSON marshals the decimal

func (*LegacyDec) MarshalTo

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

MarshalTo implements the gogo proto custom type interface.

func (LegacyDec) MarshalYAML

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

MarshalYAML returns the YAML representation.

func (LegacyDec) Mul

func (d LegacyDec) Mul(d2 LegacyDec) LegacyDec

multiplication

func (LegacyDec) MulInt

func (d LegacyDec) MulInt(i Int) LegacyDec

multiplication

func (LegacyDec) MulInt64

func (d LegacyDec) MulInt64(i int64) LegacyDec

MulInt64 - multiplication with int64

func (LegacyDec) MulInt64Mut

func (d LegacyDec) MulInt64Mut(i int64) LegacyDec

func (LegacyDec) MulIntMut

func (d LegacyDec) MulIntMut(i Int) LegacyDec

func (LegacyDec) MulMut

func (d LegacyDec) MulMut(d2 LegacyDec) LegacyDec

mutable multiplication

func (LegacyDec) MulTruncate

func (d LegacyDec) MulTruncate(d2 LegacyDec) LegacyDec

multiplication truncate

func (LegacyDec) MulTruncateMut

func (d LegacyDec) MulTruncateMut(d2 LegacyDec) LegacyDec

mutable multiplication truncage

func (LegacyDec) MustFloat64

func (d LegacyDec) MustFloat64() float64

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

func (LegacyDec) Neg

func (d LegacyDec) Neg() LegacyDec

func (LegacyDec) NegMut

func (d LegacyDec) NegMut() LegacyDec

func (LegacyDec) Power

func (d LegacyDec) Power(power uint64) LegacyDec

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

func (LegacyDec) PowerMut

func (d LegacyDec) PowerMut(power uint64) LegacyDec

func (LegacyDec) Quo

func (d LegacyDec) Quo(d2 LegacyDec) LegacyDec

quotient

func (LegacyDec) QuoInt

func (d LegacyDec) QuoInt(i Int) LegacyDec

quotient

func (LegacyDec) QuoInt64

func (d LegacyDec) QuoInt64(i int64) LegacyDec

QuoInt64 - quotient with int64

func (LegacyDec) QuoInt64Mut

func (d LegacyDec) QuoInt64Mut(i int64) LegacyDec

func (LegacyDec) QuoIntMut

func (d LegacyDec) QuoIntMut(i Int) LegacyDec

func (LegacyDec) QuoMut

func (d LegacyDec) QuoMut(d2 LegacyDec) LegacyDec

mutable quotient

func (LegacyDec) QuoRoundUp

func (d LegacyDec) QuoRoundUp(d2 LegacyDec) LegacyDec

quotient, round up

func (LegacyDec) QuoRoundupMut

func (d LegacyDec) QuoRoundupMut(d2 LegacyDec) LegacyDec

mutable quotient, round up

func (LegacyDec) QuoTruncate

func (d LegacyDec) QuoTruncate(d2 LegacyDec) LegacyDec

quotient truncate

func (LegacyDec) QuoTruncateMut

func (d LegacyDec) QuoTruncateMut(d2 LegacyDec) LegacyDec

mutable quotient truncate

func (LegacyDec) RoundInt

func (d LegacyDec) RoundInt() Int

RoundInt round the decimal using bankers rounding

func (LegacyDec) RoundInt64

func (d LegacyDec) RoundInt64() int64

RoundInt64 rounds the decimal using bankers rounding

func (LegacyDec) Set

func (d LegacyDec) Set(d2 LegacyDec) LegacyDec

func (LegacyDec) SetInt64

func (d LegacyDec) SetInt64(i int64) LegacyDec

func (*LegacyDec) Size

func (d *LegacyDec) Size() int

Size implements the gogo proto custom type interface.

func (LegacyDec) String

func (d LegacyDec) String() string

func (LegacyDec) Sub

func (d LegacyDec) Sub(d2 LegacyDec) LegacyDec

subtraction

func (LegacyDec) SubMut

func (d LegacyDec) SubMut(d2 LegacyDec) LegacyDec

mutable subtraction

func (LegacyDec) TruncateDec

func (d LegacyDec) TruncateDec() LegacyDec

TruncateDec truncates the decimals from the number and returns a Dec

func (LegacyDec) TruncateInt

func (d LegacyDec) TruncateInt() Int

TruncateInt truncates the decimals from the number and returns an Int

func (LegacyDec) TruncateInt64

func (d LegacyDec) TruncateInt64() int64

TruncateInt64 truncates the decimals from the number and returns an int64

func (*LegacyDec) Unmarshal

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

Unmarshal implements the gogo proto custom type interface.

func (*LegacyDec) UnmarshalAmino

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

func (*LegacyDec) UnmarshalJSON

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

UnmarshalJSON defines custom decoding scheme

type Uint

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

Uint wraps integer with 256 bit range bound Checks overflow, underflow and division by zero Exists in range from 0 to 2^256-1

func MaxUint

func MaxUint(u1, u2 Uint) Uint

Return the maximum of the Uints

func MinUint

func MinUint(u1, u2 Uint) Uint

Return the minimum of the Uints

func NewUint

func NewUint(n uint64) Uint

NewUint constructs Uint from int64

func NewUintFromBigInt

func NewUintFromBigInt(i *big.Int) Uint

NewUintFromBigUint constructs Uint from big.Uint

func NewUintFromString

func NewUintFromString(s string) Uint

NewUintFromString constructs Uint from string

func OneUint

func OneUint() Uint

OneUint returns Uint value with one.

func ParseUint

func ParseUint(s string) (Uint, error)

ParseUint reads a string-encoded Uint value and return a Uint.

func RelativePow

func RelativePow(x Uint, n Uint, b Uint) (z Uint)

RelativePow raises x to the power of n, where x (and the result, z) are scaled by factor b for example, RelativePow(210, 2, 100) = 441 (2.1^2 = 4.41)

func ZeroUint

func ZeroUint() Uint

ZeroUint returns unsigned zero.

func (Uint) Add

func (u Uint) Add(u2 Uint) Uint

Add adds Uint from another

func (Uint) AddUint64

func (u Uint) AddUint64(u2 uint64) Uint

Add convert uint64 and add it to Uint

func (Uint) BigInt

func (u Uint) BigInt() *big.Int

BigInt converts Uint to big.Int

func (Uint) Decr

func (u Uint) Decr() Uint

Decr decrements the Uint by one. Decr will panic if the Uint is zero.

func (Uint) Equal

func (u Uint) Equal(u2 Uint) bool

Equal compares two Uints

func (Uint) GT

func (u Uint) GT(u2 Uint) bool

GT returns true if first Uint is greater than second

func (Uint) GTE

func (u Uint) GTE(u2 Uint) bool

GTE returns true if first Uint is greater than second

func (Uint) Incr

func (u Uint) Incr() Uint

Incr increments the Uint by one.

func (Uint) IsNil

func (u Uint) IsNil() bool

IsNil returns true if Uint is uninitialized

func (Uint) IsZero

func (u Uint) IsZero() bool

IsZero returns 1 if the uint equals to 0.

func (Uint) LT

func (u Uint) LT(u2 Uint) bool

LT returns true if first Uint is lesser than second

func (Uint) LTE

func (u Uint) LTE(u2 Uint) bool

LTE returns true if first Uint is lesser than or equal to the second

func (Uint) Marshal

func (u Uint) Marshal() ([]byte, error)

Marshal implements the gogo proto custom type interface.

func (Uint) MarshalAmino

func (u Uint) MarshalAmino() ([]byte, error)

Override Amino binary serialization by proxying to protobuf.

func (Uint) MarshalJSON

func (u Uint) MarshalJSON() ([]byte, error)

MarshalJSON defines custom encoding scheme

func (*Uint) MarshalTo

func (u *Uint) MarshalTo(data []byte) (n int, err error)

MarshalTo implements the gogo proto custom type interface.

func (Uint) Mod

func (u Uint) Mod(u2 Uint) Uint

Mod returns remainder after dividing with Uint

func (Uint) Mul

func (u Uint) Mul(u2 Uint) (res Uint)

Mul multiplies two Uints

func (Uint) MulUint64

func (u Uint) MulUint64(u2 uint64) (res Uint)

Mul multiplies two Uints

func (Uint) Quo

func (u Uint) Quo(u2 Uint) (res Uint)

Quo divides Uint with Uint

func (Uint) QuoUint64

func (u Uint) QuoUint64(u2 uint64) Uint

Quo divides Uint with uint64

func (*Uint) Size

func (u *Uint) Size() int

Size implements the gogo proto custom type interface.

func (Uint) String

func (u Uint) String() string

Human readable string

func (Uint) Sub

func (u Uint) Sub(u2 Uint) Uint

Sub adds Uint from another

func (Uint) SubUint64

func (u Uint) SubUint64(u2 uint64) Uint

SubUint64 adds Uint from another

func (Uint) Uint64

func (u Uint) Uint64() uint64

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

func (*Uint) Unmarshal

func (u *Uint) Unmarshal(data []byte) error

Unmarshal implements the gogo proto custom type interface.

func (*Uint) UnmarshalAmino

func (u *Uint) UnmarshalAmino(bz []byte) error

func (*Uint) UnmarshalJSON

func (u *Uint) 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