num

package
v0.76.0-preview.7 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 License: AGPL-3.0 Imports: 8 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AbsV

func AbsV[T Signed](a T) T

AbsV generic absolute value function of signed primitives.

func CmpV added in v0.71.0

func CmpV[T1 Num, T2 Num](a T1, b T2) bool

CmpV compares 2 numeric values of any type, we attempt to cast T1 to T2 and back to see if that conversion loses any information, if no data is lost this way, we compare both values as T2, otherwise we compare both as T1.

func IntToString

func IntToString(u *Int) string

func MaxAbs added in v0.71.0

func MaxAbs[T Signed](vals ...T) T

MaxAbs - get max value based on absolute values of abolute vals.

func MaxV

func MaxV[T Num](a, b T) T

MaxV generic max of any numeric values.

func MinV

func MinV[T Num](a, b T) T

MinV generic min of numneric values.

func NumericToString added in v0.65.0

func NumericToString(n *Numeric) string

func UintFromDecimalWithFraction added in v0.72.6

func UintFromDecimalWithFraction(d Decimal) (*Uint, Decimal)

func UintToString

func UintToString(u *Uint) string

UintToString convert a uint to uint64 return "0" if nil.

func UintToUint64

func UintToUint64(u *Uint) uint64

UintToUint64 convert a uint to uint64 return 0 if nil.

Types

type Decimal

type Decimal = decimal.Decimal

func DecimalE added in v0.73.0

func DecimalE() Decimal

func DecimalFromFloat

func DecimalFromFloat(v float64) Decimal

func DecimalFromInt

func DecimalFromInt(u *Int) Decimal

func DecimalFromInt64

func DecimalFromInt64(i int64) Decimal

func DecimalFromString

func DecimalFromString(s string) (Decimal, error)

func DecimalFromUint

func DecimalFromUint(u *Uint) Decimal

func DecimalMinusOne added in v0.73.0

func DecimalMinusOne() Decimal

func DecimalOne added in v0.60.0

func DecimalOne() Decimal

func DecimalPart added in v0.72.6

func DecimalPart(a Decimal) Decimal

func DecimalTwo added in v0.73.0

func DecimalTwo() Decimal

func DecimalZero

func DecimalZero() Decimal

func MaxD

func MaxD(a, b Decimal) Decimal

func MaxDecimal

func MaxDecimal() Decimal

func Mean added in v0.73.0

func Mean(numbers []Decimal) (Decimal, error)

calculates the mean of a given slice.

func MinD

func MinD(a, b Decimal) Decimal

func MustDecimalFromString

func MustDecimalFromString(f string) Decimal

func NewDecimalFromBigInt

func NewDecimalFromBigInt(value *big.Int, exp int32) Decimal

func NewDecimalFromFloat

func NewDecimalFromFloat(f float64) Decimal

func UnmarshalBinaryDecimal added in v0.73.0

func UnmarshalBinaryDecimal(data []byte) (Decimal, error)

func Variance added in v0.73.0

func Variance(numbers []Decimal) (Decimal, error)

calculates the variance of a decimal slice.

type Int

type Int struct {
	// The unsigned version of the integer
	U *Uint
	// contains filtered or unexported fields
}

Int a wrapper to a signed big int.

func IntFromBig

func IntFromBig(b *big.Int) (*Int, bool)

IntFromBig construct a new Int with a big.Int returns true if overflow happened.

func IntFromDecimal added in v0.73.0

func IntFromDecimal(d Decimal) (*Int, bool)

IntFromDecimal returns the Int part of a decimal.

func IntFromString

func IntFromString(str string, base int) (*Int, bool)

IntFromString creates a new Int from a string interpreted using the give base. A big.Int is used to read the string, so all error related to big.Int parsing applied here. will return true if an error happened.

func IntFromUint

func IntFromUint(u *Uint, s bool) *Int

func IntZero

func IntZero() *Int

func NewInt

func NewInt(val int64) *Int

NewInt creates a new Int with the value of the int64 passed as a parameter.

func NewIntFromUint

func NewIntFromUint(val *Uint) *Int

NewIntFromUint creates a new Int with the value of the uint passed as a parameter.

func (*Int) Add

func (i *Int) Add(a *Int) *Int

Add will add the passed in value to the base value i = i + a.

func (*Int) AddSum

func (i *Int) AddSum(vals ...*Int) *Int

AddSum adds all of the parameters to i i = i + a + b + c.

func (Int) Clone

func (i Int) Clone() *Int

Clone creates a copy of the object so nothing is shared.

func (*Int) Div added in v0.73.0

func (i *Int) Div(m *Int) *Int

Mul will divide the passed in value to the base value i = i / m.

func (Int) EQ

func (i Int) EQ(o *Int) bool

func (*Int) FlipSign

func (i *Int) FlipSign()

FlipSign changes the sign of the number from - to + and back again.

func (Int) GT

func (i Int) GT(o *Int) bool

GT returns if i > o.

func (Int) GTE

func (i Int) GTE(o *Int) bool

func (Int) Int64

func (i Int) Int64() int64

func (*Int) IsNegative

func (i *Int) IsNegative() bool

IsNegative tests if the stored value is negative true if < 0 false if >= 0.

func (*Int) IsPositive

func (i *Int) IsPositive() bool

IsPositive tests if the stored value is positive true if > 0 false if <= 0.

func (*Int) IsZero

func (i *Int) IsZero() bool

IsZero tests if the stored value is zero true if == 0.

func (Int) LT

func (i Int) LT(o *Int) bool

LT returns if i < o.

func (Int) LTE

func (i Int) LTE(o *Int) bool

func (*Int) Mul added in v0.73.0

func (i *Int) Mul(m *Int) *Int

Mul will multiply the passed in value to the base value i = i * m.

func (Int) String

func (i Int) String() string

String returns a string version of the number.

func (*Int) Sub

func (i *Int) Sub(a *Int) *Int

Sub will subtract the passed in value from the base value i = i - a.

func (*Int) SubSum

func (i *Int) SubSum(vals ...*Int) *Int

SubSum subtracts all of the parameters from i i = i - a - b - c.

type Num

type Num interface {
	Signed | Unsigned
}

type Numeric added in v0.65.0

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

func NumericFromString added in v0.65.0

func NumericFromString(s string) (*Numeric, error)

func (*Numeric) Clone added in v0.65.0

func (n *Numeric) Clone() *Numeric

func (*Numeric) Decimal added in v0.65.0

func (n *Numeric) Decimal() *Decimal

func (*Numeric) Int added in v0.73.0

func (n *Numeric) Int() *Int

func (*Numeric) IsDecimal added in v0.65.0

func (n *Numeric) IsDecimal() bool

func (*Numeric) IsInt added in v0.73.0

func (n *Numeric) IsInt() bool

func (*Numeric) IsUint added in v0.65.0

func (n *Numeric) IsUint() bool

func (*Numeric) ScaleTo added in v0.65.0

func (n *Numeric) ScaleTo(op, tdp int64) (*Uint, error)

ScaleTo calculates the current contained value - decimal or uint - scaled to the target decimals.

func (*Numeric) SetDecimal added in v0.65.0

func (n *Numeric) SetDecimal(d *Decimal) *Numeric

func (*Numeric) SetInt added in v0.73.0

func (n *Numeric) SetInt(in *Int) *Numeric

func (*Numeric) SetUint added in v0.65.0

func (n *Numeric) SetUint(u *Uint) *Numeric

func (*Numeric) String added in v0.65.0

func (n *Numeric) String() string

func (*Numeric) SupportDecimalPlaces added in v0.65.0

func (n *Numeric) SupportDecimalPlaces(dp int64) bool

func (*Numeric) Uint added in v0.65.0

func (n *Numeric) Uint() *Uint

type Signed

type Signed interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 | ~float32 | ~float64
}

type Uint

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

Uint A wrapper for a big unsigned int.

func Max

func Max(a, b *Uint) *Uint

Max returns the largest of the 2 numbers.

func MaxUint

func MaxUint() *Uint

MaxUint returns max value for uint256.

func Median added in v0.74.0

func Median(nums []*Uint) *Uint

Median calculates the median of the slice of uints. it is assumed that no nils are allowed, no zeros are allowed.

func Min

func Min(a, b *Uint) *Uint

Min returns the smallest of the 2 numbers.

func MustUintFromBig added in v0.73.0

func MustUintFromBig(b *big.Int) *Uint

UintFromBig construct a new Uint with a big.Int panics if overflow happened.

func MustUintFromString added in v0.73.0

func MustUintFromString(str string, base int) *Uint

MustUintFromString creates a new Uint from a string interpreted using the given base. A big.Int is used to read the string, so all errors related to big.Int parsing are applied here. The core will panic if an error/overflow happens.

func NewUint

func NewUint(val uint64) *Uint

NewUint creates a new Uint with the value of the uint64 passed as a parameter.

func Sum

func Sum(vals ...*Uint) *Uint

Sum just removes the need to write num.NewUint(0).Sum(x, y, z) so you can write num.Sum(x, y, z) instead, equivalent to x + y + z.

func UintFromBig

func UintFromBig(b *big.Int) (*Uint, bool)

UintFromBig construct a new Uint with a big.Int returns true if overflow happened.

func UintFromBytes

func UintFromBytes(b []byte) *Uint

UintFromBytes allows for the conversion from Uint.Bytes() back to a Uint.

func UintFromDecimal

func UintFromDecimal(d Decimal) (*Uint, bool)

UintFromDecimal returns a decimal version of the Uint, setting the bool to true if overflow occurred.

func UintFromHex

func UintFromHex(hex string) (*Uint, error)

UintFromHex instantiate a uint from and hex string.

func UintFromString

func UintFromString(str string, base int) (*Uint, bool)

UintFromString created a new Uint from a string interpreted using the give base. A big.Int is used to read the string, so all error related to big.Int parsing applied here. will return true if an error/overflow happened.

func UintFromUint64 added in v0.73.0

func UintFromUint64(ui uint64) *Uint

UintFromUint64 allows for the conversion from uint64.

func UintOne

func UintOne() *Uint

func UintZero

func UintZero() *Uint

func (*Uint) Add

func (u *Uint) Add(x, y *Uint) *Uint

Add will add x and y then store the result into u this is equivalent to: `u = x + y` u is returned for convenience, no new variable is created.

func (*Uint) AddOverflow

func (u *Uint) AddOverflow(x, y *Uint) (*Uint, bool)

AddOverflow will subtract y to x then store the result into u this is equivalent to: `u = x - y` u is returned for convenience, no new variable is created. False is returned if an overflow occurred.

func (*Uint) AddSum

func (u *Uint) AddSum(vals ...*Uint) *Uint

AddSum adds multiple values at the same time to a given uint so x.AddSum(y, z) is equivalent to x + y + z.

func (*Uint) AddUint64

func (u *Uint) AddUint64(x *Uint, y uint64) *Uint

AddUint64 will add x and y then store the result into u this is equivalent to: `u = x + y` u is returned for convenience, no new variable is created.

func (Uint) BigInt

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

func (Uint) Bytes

func (u Uint) Bytes() [32]byte

Bytes return the internal representation of the Uint as [32]bytes, BigEndian encoded array.

func (Uint) Clone

func (u Uint) Clone() *Uint

Clone create copy of this value this is the equivalent to: x := u.

func (*Uint) Copy

func (u *Uint) Copy(x *Uint) *Uint

Copy create a copy of the uint this if the equivalent to: u = x.

func (*Uint) Delta

func (u *Uint) Delta(x, y *Uint) (*Uint, bool)

Delta will subtract y from x and store the result unless x-y overflowed, in which case the neg field will be set and the result of y - x is set instead.

func (*Uint) DeltaI

func (u *Uint) DeltaI(x, y *Uint) *Int

DeltaI will subtract y from x and store the result.

func (*Uint) Div

func (u *Uint) Div(x, y *Uint) *Uint

Div will divide x by y then store the result into u this is equivalent to: `u = x / y` u is returned for convenience, no new variable is created.

func (Uint) EQ

func (u Uint) EQ(oth *Uint) bool

EQ with check if the value stored in u is equal to oth this is equivalent to: `u == oth`.

func (Uint) EQUint64

func (u Uint) EQUint64(oth uint64) bool

EQUint64 with check if the value stored in u is equal to oth this is equivalent to: `u == oth`.

func (*Uint) Exp

func (u *Uint) Exp(x, y *Uint) *Uint

func (Uint) Float64

func (u Uint) Float64() float64

func (Uint) Format

func (u Uint) Format(s fmt.State, ch rune)

Format implement fmt.Formatter.

func (Uint) GT

func (u Uint) GT(oth *Uint) bool

GT with check if the value stored in u is greater than oth this is equivalent to: `u > oth`.

func (Uint) GTE

func (u Uint) GTE(oth *Uint) bool

GTE with check if the value stored in u is greater than or equal to oth this is equivalent to: `u >= oth`.

func (Uint) GTEUint64

func (u Uint) GTEUint64(oth uint64) bool

GTEUint64 with check if the value stored in u is greater than or equal to oth this is equivalent to: `u >= oth`.

func (Uint) GTUint64

func (u Uint) GTUint64(oth uint64) bool

GTUint64 with check if the value stored in u is greater than oth this is equivalent to: `u > oth`.

func (Uint) Hex

func (u Uint) Hex() string

Hex returns the hexadecimal representation of the stored value.

func (Uint) IsNegative

func (u Uint) IsNegative() bool

IsNegative returns whether the value is < 0.

func (Uint) IsZero

func (u Uint) IsZero() bool

IsZero return whether u == 0 or not.

func (Uint) LT

func (u Uint) LT(oth *Uint) bool

LT with check if the value stored in u is lesser than oth this is equivalent to: `u < oth`.

func (Uint) LTE

func (u Uint) LTE(oth *Uint) bool

LTE with check if the value stored in u is lesser than or equal to oth this is equivalent to: `u <= oth`.

func (Uint) LTEUint64

func (u Uint) LTEUint64(oth uint64) bool

LTEUint64 with check if the value stored in u is lesser than or equal to oth this is equivalent to: `u <= oth`.

func (Uint) LTUint64

func (u Uint) LTUint64(oth uint64) bool

LTUint64 with check if the value stored in u is lesser than oth this is equivalent to: `u < oth`.

func (Uint) MarshalBinary added in v0.74.0

func (u Uint) MarshalBinary() (data []byte, err error)

MarshalBinary implements the encoding.BinaryMarshaler interface.

func (Uint) MarshalJSON added in v0.74.0

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

MarshalJSON implements the json.Marshaler interface.

func (*Uint) Mod

func (u *Uint) Mod(x, y *Uint) *Uint

Mod sets u to the modulus x%y for y != 0 and returns u. If y == 0, u is set to 0.

func (*Uint) Mul

func (u *Uint) Mul(x, y *Uint) *Uint

Mul will multiply x and y then store the result into u this is equivalent to: `u = x * y` u is returned for convenience, no new variable is created.

func (Uint) NEQ

func (u Uint) NEQ(oth *Uint) bool

NEQ with check if the value stored in u is different than oth this is equivalent to: `u != oth`.

func (Uint) NEQUint64

func (u Uint) NEQUint64(oth uint64) bool

NEQUint64 with check if the value stored in u is different than oth this is equivalent to: `u != oth`.

func (*Uint) Scan added in v0.74.0

func (u *Uint) Scan(value interface{}) error

Scan implements the sql.Scanner interface for database deserialization.

func (*Uint) Set

func (u *Uint) Set(oth *Uint) *Uint

func (*Uint) SetUint64

func (u *Uint) SetUint64(val uint64) *Uint

func (Uint) String

func (u Uint) String() string

String returns the stored value as a string this is internally using big.Int.String().

func (*Uint) Sub

func (u *Uint) Sub(x, y *Uint) *Uint

Sub will subtract y from x then store the result into u this is equivalent to: `u = x - y` u is returned for convenience, no new variable is created.

func (*Uint) SubOverflow

func (u *Uint) SubOverflow(x, y *Uint) (*Uint, bool)

SubOverflow will subtract y to x then store the result into u this is equivalent to: `u = x - y` u is returned for convenience, no new variable is created. False is returned if an overflow occurred.

func (*Uint) ToDecimal

func (u *Uint) ToDecimal() Decimal

ToDecimal returns the value of the Uint as a Decimal.

func (Uint) Uint64

func (u Uint) Uint64() uint64

func (*Uint) UnmarshalBinary added in v0.74.0

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

UnmarshalBinary implements the encoding.BinaryUnmarshaler interface. As a string representation is already used when encoding to text, this method stores that string as []byte.

func (*Uint) UnmarshalJSON added in v0.74.0

func (u *Uint) UnmarshalJSON(numericBytes []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (Uint) Value added in v0.74.0

func (u Uint) Value() (driver.Value, error)

Value implements the driver.Valuer interface for database serialization.

type Unsigned

type Unsigned interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
}

type WrappedDecimal

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

func NewWrappedDecimal

func NewWrappedDecimal(integerRepresentation *Uint, underlyingValue Decimal) WrappedDecimal

NewWrappedDecimal returns a new instance of a decimal coupled with the Uint representation that has been chosen for it.

func (WrappedDecimal) Original

func (w WrappedDecimal) Original() Decimal

Original returns the underlying decimal value.

func (WrappedDecimal) Representation

func (w WrappedDecimal) Representation() *Uint

Representation returns the Uint representation of a decimal that has been affixed when calling the constructor.

Jump to

Keyboard shortcuts

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