safetype

package
v0.0.0-...-43c3cf5 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2021 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Uint128Bytes the number of bytes Uint128 type will take.
	Uint128Bytes = 16
	// Uint128Bits defines the number of bits for Uint128 type.
	Uint128Bits = 128
)

Variables

View Source
var (
	// ErrUint128Overflow indicates the value is greater than maximum of 2^128-1.
	ErrUint128Overflow = errors.New("Uint128: overflow")

	// ErrUint128Underflow indicates the value is less than minimum of 0.
	ErrUint128Underflow = errors.New("Uint128: underflow")

	// ErrUint128InvalidBytes indicates the bytes size is not equal to Uint128Bytes.
	ErrUint128InvalidBytes = errors.New("Uint128: invalid bytes")

	// ErrUint128InvalidString indicates the string is not valid when converted to Uint128.
	ErrUint128InvalidString = errors.New("Uint128: invalid string")

	// ErrUint128DividedByZero indicates a division-by-zero error occurs
	ErrUint128DividedByZero = errors.New("Uint128: divided by zero")
)

Functions

This section is empty.

Types

type Uint128

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

Uint128 defines a uint128 type with safe arithmetic operations implemented, based on big.Int.

Supported arithmetic operations:

u1 = u1.Add(u2)
u1 = u1.Sub(u2)
u1 = u1.Mul(u2)
u1 = u1.Div(u2)
u1 = u1.Mod(u2)
u1 = u1.Exp(u2)
etc.

func NewUint128

func NewUint128() *Uint128

NewUint128 returns a new Uint128 struct with default value 0.

func NewUint128FromBigInt

func NewUint128FromBigInt(i *big.Int) (*Uint128, error)

NewUint128FromBigInt returns a new Uint128 struct with given value and value check.

func NewUint128FromBytes

func NewUint128FromBytes(bytes []byte) (*Uint128, error)

NewUint128FromBytes converts big-endian byte slice to Uint128, len(bytes) must be not greater than Uint128Bytes.

func NewUint128FromInt

func NewUint128FromInt(i int64) (*Uint128, error)

NewUint128FromInt returns a new Uint128 struct with given value and value check.

func NewUint128FromString

func NewUint128FromString(str string) (*Uint128, error)

NewUint128FromString returns a new Uint128 struct with given value and value check.

func NewUint128FromUint

func NewUint128FromUint(i uint64) *Uint128

NewUint128FromUint returns a new Uint128 with given value

func (*Uint128) AbsBytes

func (u *Uint128) AbsBytes() []byte

RawBytes returns the underlying byte slice of u with prefix 0(byte) removed.

func (*Uint128) Add

func (u *Uint128) Add(x *Uint128) (*Uint128, error)

Add returns u + x

func (*Uint128) AddInt

func (u *Uint128) AddInt(i int64) (*Uint128, error)

func (*Uint128) AddUint

func (u *Uint128) AddUint(i uint64) (*Uint128, error)

func (*Uint128) BigValue

func (u *Uint128) BigValue() *big.Int

func (*Uint128) Bytes

func (u *Uint128) Bytes() [Uint128Bytes]byte

Bytes returns the value of u as a big-endian byte array.

func (*Uint128) Cmp

func (u *Uint128) Cmp(x *Uint128) int

Cmp compares u and x and returns:

-1 if u <  x
 0 if u == x
+1 if u >  x

func (*Uint128) DeepCopy

func (u *Uint128) DeepCopy() *Uint128

DeepCopy returns a deep copy of u

func (*Uint128) Div

func (u *Uint128) Div(x *Uint128) (*Uint128, error)

Div returns u / x.

func (*Uint128) DivInt

func (u *Uint128) DivInt(i int64) (*Uint128, error)

func (*Uint128) Eq

func (u *Uint128) Eq(x *Uint128) bool

Eq returns whether u is equal to x

func (*Uint128) Exp

func (u *Uint128) Exp(x *Uint128) (*Uint128, error)

Exp returns u^x

func (*Uint128) Float64

func (u *Uint128) Float64() float64

func (*Uint128) Ge

func (u *Uint128) Ge(x *Uint128) bool

Gt returns whether u is greater than or equal to x

func (*Uint128) Gt

func (u *Uint128) Gt(x *Uint128) bool

Gt returns whether u is greater than x

func (*Uint128) IntValue

func (u *Uint128) IntValue() int64

IntValue returns the int64 representation of u. Panic if u is greater than maximum int64

func (*Uint128) IsZero

func (u *Uint128) IsZero() bool

func (*Uint128) Le

func (u *Uint128) Le(x *Uint128) bool

Le returns whether u is less than or equal to x

func (*Uint128) Lt

func (u *Uint128) Lt(x *Uint128) bool

Lt returns whether u is less than x

func (*Uint128) Mod

func (u *Uint128) Mod(x *Uint128) (*Uint128, error)

Mod returns u % x

func (*Uint128) Mul

func (u *Uint128) Mul(x *Uint128) (*Uint128, error)

Mul returns u * x

func (*Uint128) MulInt

func (u *Uint128) MulInt(i int64) (*Uint128, error)

func (*Uint128) Rsh

func (u *Uint128) Rsh(n uint) *Uint128

Rsh returns u>>n

func (*Uint128) String

func (u *Uint128) String() string

String returns the string representation of x.

func (*Uint128) Sub

func (u *Uint128) Sub(x *Uint128) (*Uint128, error)

Sub returns u - x

func (*Uint128) SubUint

func (u *Uint128) SubUint(x uint64) (*Uint128, error)

func (*Uint128) UintValue

func (u *Uint128) UintValue() uint64

UintValue returns the uint64 representation of u. Panic if u is greater than maximum uint64

func (*Uint128) Validate

func (u *Uint128) Validate() error

Validate returns error if u is not a valid Uint128, otherwise returns nil.

Jump to

Keyboard shortcuts

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