math

package
v1.7.4 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2024 License: Apache-2.0 Imports: 4 Imported by: 6

Documentation

Overview

Package math provides integer math utilities.

Index

Constants

View Source
const (
	MaxInt8   = 1<<7 - 1
	MinInt8   = -1 << 7
	MaxInt16  = 1<<15 - 1
	MinInt16  = -1 << 15
	MaxInt32  = 1<<31 - 1
	MinInt32  = -1 << 31
	MaxInt64  = 1<<63 - 1
	MinInt64  = -1 << 63
	MaxUint8  = 1<<8 - 1
	MaxUint16 = 1<<16 - 1
	MaxUint32 = 1<<32 - 1
	MaxUint64 = 1<<64 - 1
)

Integer limit values.

Variables

View Source
var (
	MaxBig256 = new(big.Int).Set(tt256m1)
	MaxBig63  = new(big.Int).Sub(tt63, big.NewInt(1))

	Big1         = big.NewInt(1)
	Big2         = big.NewInt(2)
	Big3         = big.NewInt(3)
	Big0         = big.NewInt(0)
	Big32        = big.NewInt(32)
	Big256       = big.NewInt(256)
	Big257       = big.NewInt(257)
	BigMaxUint32 = big.NewInt(MaxUint32)
	BigMaxUint64 = new(big.Int).SetUint64(MaxUint64)

	Rat1 = big.NewRat(1, 1)
)

Various big integer limit values.

Functions

func AddBigInt added in v1.3.6

func AddBigInt(base, delta *big.Int) *big.Int

func BigForPrint added in v1.3.6

func BigForPrint(x *big.Int) string

func BigIntForPrint

func BigIntForPrint(x *big.Int) string

func BigMax

func BigMax(x, y *big.Int) *big.Int

BigMax returns the larger of x or y.

func BigMin

func BigMin(x, y *big.Int) *big.Int

BigMin returns the smaller of x or y.

func BigPow

func BigPow(a, b int64) *big.Int

BigPow returns a ** b as a big integer.

func BigRatForPrint added in v1.3.6

func BigRatForPrint(x *big.Rat) string

func BigStringForPrint added in v1.3.6

func BigStringForPrint(s string) string

func Byte

func Byte(bigint *big.Int, padlength, n int) byte

Byte returns the byte at position n, with the supplied padlength in Little-Endian encoding. n==0 returns the MSB Example: bigint '5', padlength 32, n=31 => 5

func CompareBigInt

func CompareBigInt(a, b *big.Int) int

func CompareBigRat added in v1.3.6

func CompareBigRat(a, b *big.Rat) int

func CopyBigInt

func CopyBigInt(x *big.Int) *big.Int

func CopyBigRat added in v1.3.6

func CopyBigRat(x *big.Rat) *big.Rat

func Exp

func Exp(base, exponent *big.Int) *big.Int

Exp implements exponentiation by squaring. Exp returns a newly-allocated big integer and does not change base or exponent. The result is truncated to 256 bits.

Courtesy @karalabe and @chfast

func FirstBitSet

func FirstBitSet(v *big.Int) int

FirstBitSet returns the index of the first 1 bit in v, counting from LSB.

func Int64MulRat

func Int64MulRat(i64 int64, r *big.Rat) (x int64)

r not bigger than 1 x returns the smallest integer not less than i64*r

func MustBigInt

func MustBigInt(x *big.Int) *big.Int

func MustCreatedBigInt added in v1.3.6

func MustCreatedBigInt(v *big.Int, isCreated bool) *big.Int

func MustParseBig256

func MustParseBig256(s string) *big.Int

MustParseBig256 parses s as a 256 bit big integer and panics if the string is invalid.

func MustParseUint64

func MustParseUint64(s string) uint64

MustParseUint64 parses s as an integer and panics if the string is invalid.

func MustPositiveInt added in v1.3.30

func MustPositiveInt(x *big.Int) *big.Int

func PaddedBigBytes

func PaddedBigBytes(bigint *big.Int, n int) []byte

PaddedBigBytes encodes a big integer as a big-endian byte slice. The length of the slice is at least n bytes.

func ParseBig256

func ParseBig256(s string) (*big.Int, bool)

ParseBig256 parses s as a 256 bit integer in decimal or hexadecimal syntax. Leading zeros are accepted. The empty string parses as zero.

func ParseUint64

func ParseUint64(s string) (uint64, bool)

ParseUint64 parses s as an integer in decimal or hexadecimal syntax. Leading zeros are accepted. The empty string parses as zero.

func RatToInt64

func RatToInt64(r *big.Rat) (x int64, nomeans bool)

return the smallest integer not less than r. If r is too large or divided by 0, nomeans is true, and x is meaningless

func RatToInt64Floor

func RatToInt64Floor(r *big.Rat) (x int64, nomeans bool)

lower round of r

func ReadBits

func ReadBits(bigint *big.Int, buf []byte)

ReadBits encodes the absolute value of bigint as big-endian bytes. Callers must ensure that buf has enough space. If buf is too short the result will be incomplete.

func S256

func S256(x *big.Int) *big.Int

S256 interprets x as a two's complement number. x must not exceed 256 bits (the result is undefined if it does) and is not modified.

S256(0)        = 0
S256(1)        = 1
S256(2**255)   = -2**255
S256(2**256-1) = -1

func SafeAdd

func SafeAdd(x, y uint64) (uint64, bool)

SafeAdd returns the result and whether overflow occurred.

func SafeMul

func SafeMul(x, y uint64) (uint64, bool)

SafeMul returns multiplication result and whether overflow occurred.

func SafeSub

func SafeSub(x, y uint64) (uint64, bool)

SafeSub returns subtraction result and whether overflow occurred.

func U256

func U256(x *big.Int) *big.Int

U256 encodes as a 256 bit two's complement number. This operation is destructive.

func U256Bytes added in v1.3.40

func U256Bytes(n *big.Int) []byte

U256Bytes converts a big Int into a 256bit EVM number. This operation is destructive.

Types

type BigInt added in v1.3.30

type BigInt big.Int

func NewBigInt added in v1.3.30

func NewBigInt(a *big.Int) *BigInt

func (*BigInt) Add added in v1.3.30

func (b *BigInt) Add(a *BigInt) *BigInt

func (*BigInt) AddInt added in v1.3.30

func (b *BigInt) AddInt(a *big.Int) *BigInt

func (*BigInt) AddInts added in v1.3.45

func (b *BigInt) AddInts(is ...*big.Int) *BigInt

func (*BigInt) Clone added in v1.3.30

func (b *BigInt) Clone() *BigInt

func (*BigInt) Compare added in v1.3.30

func (b *BigInt) Compare(o *BigInt) int

func (*BigInt) CompareInt added in v1.3.30

func (b *BigInt) CompareInt(i *big.Int) int

func (*BigInt) Div added in v1.3.31

func (b *BigInt) Div(a *BigInt) *BigInt

func (*BigInt) DivInt added in v1.3.31

func (b *BigInt) DivInt(a *big.Int) *BigInt

func (*BigInt) HexString added in v1.3.37

func (b *BigInt) HexString() string

func (*BigInt) Int added in v1.3.30

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

func (*BigInt) Mul added in v1.3.30

func (b *BigInt) Mul(a *BigInt) *BigInt

func (*BigInt) MulInt added in v1.3.31

func (b *BigInt) MulInt(a *big.Int) *BigInt

func (*BigInt) MulRat added in v1.3.31

func (b *BigInt) MulRat(r *big.Rat) *BigInt

func (*BigInt) MustInt added in v1.3.30

func (b *BigInt) MustInt() *big.Int

func (*BigInt) MustPositive added in v1.3.30

func (b *BigInt) MustPositive() *BigInt

func (*BigInt) Negative added in v1.3.31

func (b *BigInt) Negative() bool

func (*BigInt) Positive added in v1.3.30

func (b *BigInt) Positive() bool

func (*BigInt) SetInt added in v1.3.30

func (b *BigInt) SetInt(a *big.Int) *BigInt

func (*BigInt) Sign added in v1.3.31

func (b *BigInt) Sign() int

func (*BigInt) String added in v1.3.30

func (b *BigInt) String() string

func (*BigInt) Sub added in v1.3.31

func (b *BigInt) Sub(a *BigInt) *BigInt

func (*BigInt) SubInt added in v1.3.30

func (b *BigInt) SubInt(a *big.Int) *BigInt

func (*BigInt) SubIntNotLessZero added in v1.3.45

func (b *BigInt) SubIntNotLessZero(subtrahend *big.Int) (left *BigInt, actualSubtrahend *BigInt)

if b<=0, always return (b, nil) subtranhend cannot less than zero

func (*BigInt) ToUint64 added in v1.3.40

func (b *BigInt) ToUint64() (u uint64, ok bool)

type Decimal256 added in v1.3.40

type Decimal256 big.Int

Decimal256 unmarshals big.Int as a decimal string. When unmarshalling, it however accepts either "0x"-prefixed (hex encoded) or non-prefixed (decimal)

func NewDecimal256 added in v1.3.40

func NewDecimal256(x int64) *Decimal256

NewHexOrDecimal256 creates a new Decimal256

func (*Decimal256) MarshalText added in v1.3.40

func (i *Decimal256) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (*Decimal256) String added in v1.3.40

func (i *Decimal256) String() string

String implements Stringer.

func (*Decimal256) UnmarshalText added in v1.3.40

func (i *Decimal256) UnmarshalText(input []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type HexOrDecimal256

type HexOrDecimal256 big.Int

HexOrDecimal256 marshals big.Int as hex or decimal.

func NewHexOrDecimal256 added in v1.3.40

func NewHexOrDecimal256(x int64) *HexOrDecimal256

NewHexOrDecimal256 creates a new HexOrDecimal256

func (*HexOrDecimal256) MarshalText

func (i *HexOrDecimal256) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (*HexOrDecimal256) UnmarshalText

func (i *HexOrDecimal256) UnmarshalText(input []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type HexOrDecimal64

type HexOrDecimal64 uint64

HexOrDecimal64 marshals uint64 as hex or decimal.

func (HexOrDecimal64) MarshalText

func (i HexOrDecimal64) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (*HexOrDecimal64) UnmarshalText

func (i *HexOrDecimal64) UnmarshalText(input []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

Jump to

Keyboard shortcuts

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