math

package module
v1.0.0-beta.1 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2022 License: Apache-2.0 Imports: 6 Imported by: 0

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 MaxBitLen = 256

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

Variables

This section is empty.

Functions

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