math

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2022 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package math contains some human-readable mathematical primitives.

Index

Constants

View Source
const (
	// Uint128Size defines the byte size of an Uint128
	Uint128Size = 16
	// Uint128BitSize defines the bit size of an Uint128
	Uint128BitSize = 16 * 8
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Uint128

type Uint128 struct {
	Lo, Hi uint64
}

Uint128 is an unsigned 128-bit number.

func MaxUint128

func MaxUint128() Uint128

MaxUint128 returns the max value of an Uint128

func NewUint128

func NewUint128(lo, hi uint64) Uint128

NewUint128 returns the Uint128 value (lo,hi).

func NewUint128FromUint64

func NewUint128FromUint64(u uint64) Uint128

NewUint128FromUint64 returns an Uint128 from an uint64

func ZeroUint128

func ZeroUint128() Uint128

ZeroUint128 returns the zero value of an Uint128

func (Uint128) Add

func (u Uint128) Add(v Uint128) Uint128

Add returns u+v panicking on overflow.

func (Uint128) Add64

func (u Uint128) Add64(v uint64) Uint128

Add64 returns u+v panicking on overflow.

func (Uint128) AddWrap

func (u Uint128) AddWrap(v Uint128) Uint128

AddWrap returns u+v with wraparound semantics; for example, Max.AddWrap(From64(1)) == Zero.

func (Uint128) AddWrap64

func (u Uint128) AddWrap64(v uint64) Uint128

AddWrap64 returns u+v with wraparound semantics; for example, Max.AddWrap64(1) == Zero.

func (Uint128) And

func (u Uint128) And(v Uint128) Uint128

And returns u&v.

func (Uint128) And64

func (u Uint128) And64(v uint64) Uint128

And64 returns u&v.

func (Uint128) Cmp

func (u Uint128) Cmp(v Uint128) int

Cmp compares u and v and returns:

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

func (Uint128) Cmp64

func (u Uint128) Cmp64(v uint64) int

Cmp64 compares u and v and returns:

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

func (Uint128) Div

func (u Uint128) Div(v Uint128) Uint128

Div returns u/v. Panics if v is invalid.

func (Uint128) Div64

func (u Uint128) Div64(v uint64) Uint128

Div64 returns u/v. Panics if v is invalid.

func (Uint128) Equals

func (u Uint128) Equals(v Uint128) bool

Equals returns true if u == v.

Uint128 values can be compared directly with ==, but use of the Equals method is preferred for consistency.

func (Uint128) Equals64

func (u Uint128) Equals64(v uint64) bool

Equals64 returns true if u == v.

func (*Uint128) From64

func (u *Uint128) From64(v uint64)

From64 converts v to a Uint128 value.

func (*Uint128) FromBEBytes

func (u *Uint128) FromBEBytes(b []byte) error

FromBEBytes populates the Uin128 value given bytes in big endian order.

func (*Uint128) FromLEBytes

func (u *Uint128) FromLEBytes(b []byte) error

FromLEBytes populates the Uint128 value given bytes in little endian order.

func (*Uint128) FromString

func (u *Uint128) FromString(s string) error

FromString populates the Uint128 with a base0 string.

func (Uint128) IsZero

func (u Uint128) IsZero() bool

IsZero returns true if u == 0.

func (Uint128) LeadingZeros

func (u Uint128) LeadingZeros() int

LeadingZeros returns the number of leading zero bits in u; the result is 128 for u == 0.

func (Uint128) Len

func (u Uint128) Len() int

Len returns the minimum number of bits required to represent u; the result is 0 for u == 0.

func (Uint128) Lsh

func (u Uint128) Lsh(n uint) (s Uint128)

Lsh returns u<<n.

func (Uint128) MarshalJSON

func (u Uint128) MarshalJSON() (b []byte, err error)

MarshalJSON implements json.Marshaler and returns Uint128.String converted to bytes.

func (Uint128) Mod

func (u Uint128) Mod(v Uint128) (r Uint128)

Mod returns r = u%v, panicking on invalid values of v.

func (Uint128) Mod64

func (u Uint128) Mod64(v uint64) (r uint64)

Mod64 returns r = u%v, panicking on invalid values of v.

func (Uint128) Mul

func (u Uint128) Mul(v Uint128) Uint128

Mul returns u*v, panicking on overflow.

func (Uint128) Mul64

func (u Uint128) Mul64(v uint64) Uint128

Mul64 returns u*v, panicking on overflow.

func (Uint128) MulWrap

func (u Uint128) MulWrap(v Uint128) Uint128

MulWrap returns u*v with wraparound semantics; for example, Max.MulWrap(Max) == 1.

func (Uint128) MulWrap64

func (u Uint128) MulWrap64(v uint64) Uint128

MulWrap64 returns u*v with wraparound semantics; for example, Max.MulWrap64(2) == Max.Sub64(1).

func (Uint128) OnesCount

func (u Uint128) OnesCount() int

OnesCount returns the number of one bits ("population count") in u.

func (Uint128) Or

func (u Uint128) Or(v Uint128) Uint128

Or returns u|v.

func (Uint128) Or64

func (u Uint128) Or64(v uint64) Uint128

Or64 returns u|v.

func (*Uint128) PutBEBytes

func (u *Uint128) PutBEBytes(b []byte)

PutBEBytes stores u in b in big-endian order. It panics if len(b) < 16.

func (Uint128) PutLEBytes

func (u Uint128) PutLEBytes(b []byte)

PutLEBytes stores u in b in little-endian order. It panics if len(b) < 16.

func (Uint128) QuoRem

func (u Uint128) QuoRem(v Uint128) (q, r Uint128)

QuoRem returns q = u/v and r = u%v, panicking on division by zero.

func (Uint128) QuoRem64

func (u Uint128) QuoRem64(v uint64) (Uint128, uint64)

QuoRem64 returns q = u/v and r = u%v, panicking on division by zero or quotient overflow.

func (Uint128) Reverse

func (u Uint128) Reverse() Uint128

Reverse returns the value of u with its bits in reversed order.

func (Uint128) ReverseBytes

func (u Uint128) ReverseBytes() Uint128

ReverseBytes returns the value of u with its bytes in reversed order.

func (Uint128) RotateLeft

func (u Uint128) RotateLeft(k int) Uint128

RotateLeft returns the value of u rotated left by (k mod 128) bits.

func (Uint128) RotateRight

func (u Uint128) RotateRight(k int) Uint128

RotateRight returns the value of u rotated left by (k mod 128) bits.

func (Uint128) Rsh

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

Rsh returns u>>n.

func (Uint128) SafeAdd

func (u Uint128) SafeAdd(v Uint128) (Uint128, error)

SafeAdd returns u+v or an error on overflow.

func (Uint128) SafeAdd64

func (u Uint128) SafeAdd64(v uint64) (Uint128, error)

SafeAdd64 returns u+v or an error on overflow.

func (Uint128) SafeDiv

func (u Uint128) SafeDiv(v Uint128) (q Uint128, err error)

SafeDiv returns u/v or an error if v is invalid.

func (Uint128) SafeDiv64

func (u Uint128) SafeDiv64(v uint64) (q Uint128, err error)

SafeDiv64 returns u/v or an error if v is invalid.

func (Uint128) SafeMod

func (u Uint128) SafeMod(v Uint128) (r Uint128, err error)

SafeMod returns r = u%v, returning errors on invalid v.

func (Uint128) SafeMod64

func (u Uint128) SafeMod64(v uint64) (r uint64, err error)

SafeMod64 returns r = u%v, returning errors on invalid values of v.

func (Uint128) SafeMul

func (u Uint128) SafeMul(v Uint128) (Uint128, error)

SafeMul returns u*v or an error on overflow.

func (Uint128) SafeMul64

func (u Uint128) SafeMul64(v uint64) (Uint128, error)

SafeMul64 returns u*v or an error on overflow.

func (Uint128) SafeQuoRem

func (u Uint128) SafeQuoRem(v Uint128) (q Uint128, r Uint128, err error)

SafeQuoRem returns q = u/v and u%v, returning an error on division by zero or quotient overflow.

func (Uint128) SafeQuoRem64

func (u Uint128) SafeQuoRem64(v uint64) (q Uint128, r uint64, err error)

SafeQuoRem64 returns q = u/v r = u%v returning an error on division by zero or quotient overflow.

func (Uint128) SafeSub

func (u Uint128) SafeSub(v Uint128) (Uint128, error)

SafeSub returns u-v or an error on overflow.

func (Uint128) SafeSub64

func (u Uint128) SafeSub64(v uint64) (Uint128, error)

SafeSub64 returns u-v or an error on overflow.

func (Uint128) String

func (u Uint128) String() string

String returns the base-10 representation of u as a string.

func (Uint128) Sub

func (u Uint128) Sub(v Uint128) Uint128

Sub returns u-v panicking on overflow.

func (Uint128) Sub64

func (u Uint128) Sub64(v uint64) Uint128

Sub64 returns u-v panicking on overflow.

func (Uint128) SubWrap

func (u Uint128) SubWrap(v Uint128) Uint128

SubWrap returns u-v with wraparound semantics; for example, Zero.SubWrap(From64(1)) == Max.

func (Uint128) SubWrap64

func (u Uint128) SubWrap64(v uint64) Uint128

SubWrap64 returns u-v with wraparound semantics; for example, Zero.SubWrap64(1) == Max.

func (Uint128) TrailingZeros

func (u Uint128) TrailingZeros() int

TrailingZeros returns the number of trailing zero bits in u; the result is 128 for u == 0.

func (*Uint128) UnmarshalJSON

func (u *Uint128) UnmarshalJSON(b []byte) error

UnmarshalJSON populates Uint128 from a json string value.

func (Uint128) Xor

func (u Uint128) Xor(v Uint128) Uint128

Xor returns u^v.

func (Uint128) Xor64

func (u Uint128) Xor64(v uint64) Uint128

Xor64 returns u^v.

Jump to

Keyboard shortcuts

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