math

package
v0.16.3 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2022 License: GPL-3.0 Imports: 5 Imported by: 0

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))
	MaxBig104 = new(big.Int).Set(tt104m1)
)

Various big integer limit values.

View Source
var (
	DELTA         = [...]float64{0.08333333333333333, -2.777777777777778e-5, 7.936507936507937e-8, -5.952380952380953e-10, 8.417508417508329e-12, -1.917526917518546e-13, 6.410256405103255e-15, -2.955065141253382e-16, 1.7964371635940225e-17, -1.3922896466162779e-18, 1.338028550140209e-19, -1.542460098679661e-20, 1.9770199298095743e-21, -2.3406566479399704e-22, 1.713480149663986e-23}
	LANCZOS       = [...]float64{0.9999999999999971, 57.15623566586292, -59.59796035547549, 14.136097974741746, -0.4919138160976202, 3.399464998481189e-5, 4.652362892704858e-5, -9.837447530487956e-5, 1.580887032249125e-4, -2.1026444172410488e-4, 2.1743961811521265e-4, -1.643181065367639e-4, 8.441822398385275e-5, -2.6190838401581408e-5, 3.6899182659531625e-6}
	HALF_LOG_2_PI = 0.5 * math.Log(6.283185307179586)
)

Functions

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

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

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

Types

type Beta

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

func (*Beta) RegularizedBeta

func (beta *Beta) RegularizedBeta(x float64, a float64, b float64, epsilon float64, maxIterations int64) (float64, error)

func (*Beta) SimpleRegularizedBeta

func (beta *Beta) SimpleRegularizedBeta(x float64, a float64, b float64) (float64, error)

type BinomialDistribution

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

func NewBinomialDistribution

func NewBinomialDistribution(n int64, p float64) *BinomialDistribution

func (*BinomialDistribution) CumulativeProbability

func (bd *BinomialDistribution) CumulativeProbability(x int64) (float64, error)

func (*BinomialDistribution) InverseCumulativeProbability

func (bd *BinomialDistribution) InverseCumulativeProbability(p float64) (int64, error)

type ContinuedFraction

type ContinuedFraction struct {
}

type Gamma

type Gamma struct {
}

type HexOrDecimal256

type HexOrDecimal256 big.Int

HexOrDecimal256 marshals big.Int as hex or decimal.

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