Documentation ¶
Overview ¶
Package math provides integer math utilities.
Index ¶
- Constants
- Variables
- func BigMax(x, y *big.Int) *big.Int
- func BigMin(x, y *big.Int) *big.Int
- func BigPow(a, b int64) *big.Int
- func Byte(bigint *big.Int, padlength, n int) byte
- func Exp(base, exponent *big.Int) *big.Int
- func FirstBitSet(v *big.Int) int
- func MustParseBig256(s string) *big.Int
- func MustParseUint64(s string) uint64
- func PaddedBigBytes(bigint *big.Int, n int) []byte
- func ParseBig256(s string) (*big.Int, bool)
- func ParseUint64(s string) (uint64, bool)
- func ReadBits(bigint *big.Int, buf []byte)
- func S256(x *big.Int) *big.Int
- func SafeAdd(x, y uint64) (uint64, bool)
- func SafeMul(x, y uint64) (uint64, bool)
- func SafeSub(x, y uint64) (uint64, bool)
- func U256(x *big.Int) *big.Int
- type Float
- func (f Float) Big() (x *big.Float, nan bool)
- func (f Float) Bits() (a, b uint64)
- func (f Float) Exp() int
- func (f Float) Float32() (float32, big.Accuracy)
- func (f Float) Float64() (float64, big.Accuracy)
- func (f Float) Frac() (uint64, uint64)
- func (f Float) High() uint64
- func (f Float) Low() uint64
- func (f Float) Signbit() bool
- type HexOrDecimal256
- type HexOrDecimal64
Constants ¶
const ( // Precision specifies the number of bits in the mantissa (including the // implicit lead bit). F128Precision = 113 // exponent bias. F128Bias = 16383 )
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 ¶
var ( MaxBig256 = new(big.Int).Set(tt256m1) MaxBig63 = new(big.Int).Sub(tt63, big.NewInt(1)) )
Various big integer limit values.
var ( // +NaN F128NaN = Float{/* contains filtered or unexported fields */} // -NaN F128NegNaN = Float{/* contains filtered or unexported fields */} // +Inf F128Inf = Float{/* contains filtered or unexported fields */} // -Inf F128NegInf = Float{/* contains filtered or unexported fields */} // +zero F128Zero = Float{/* contains filtered or unexported fields */} // -zero F128NegZero = Float{/* contains filtered or unexported fields */} )
Positive and negative Not-a-Number, infinity and zero.
Functions ¶
func 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 ¶
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 ¶
FirstBitSet returns the index of the first 1 bit in v, counting from LSB.
func MustParseBig256 ¶
MustParseBig256 parses s as a 256 bit big integer and panics if the string is invalid.
func MustParseUint64 ¶
MustParseUint64 parses s as an integer and panics if the string is invalid.
func PaddedBigBytes ¶
PaddedBigBytes encodes a big integer as a big-endian byte slice. The length of the slice is at least n bytes.
func ParseBig256 ¶
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 ¶
ParseUint64 parses s as an integer in decimal or hexadecimal syntax. Leading zeros are accepted. The empty string parses as zero.
func ReadBits ¶
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 ¶
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
Types ¶
type Float ¶
type Float struct {
// contains filtered or unexported fields
}
Float is a floating-point number in IEEE 754 quadruple precision format.
func NewFromBig ¶
NewFromBig returns the nearest quadruple precision floating-point number for x and the accuracy of the conversion.
func NewFromBits ¶
NewFromBits returns the floating-point number corresponding to the IEEE 754 quadruple precision binary representation.
func NewFromFloat32 ¶
NewFromFloat32 returns the nearest quadruple precision floating-point number for x and the accuracy of the conversion.
func NewFromFloat64 ¶
NewFromFloat64 returns the nearest quadruple precision floating-point number for x and the accuracy of the conversion.
func (Float) Big ¶
Big returns the multi-precision floating-point number representation of f and a boolean indicating whether f is Not-a-Number.
func (Float) Float32 ¶
Float32 returns the float32 value nearest to f. If f is too small to be represented by a float32 (|f| < math.SmallestNonzeroFloat32), the result is (0, Below) or (-0, Above), respectively, depending on the sign of f. If f is too large to be represented by a float32 (|f| > math.MaxFloat32), the result is (+Inf, Above) or (-Inf, Below), depending on the sign of f.
func (Float) Float64 ¶
Float64 returns the float64 value nearest to f. If f is too small to be represented by a float64 (|f| < math.SmallestNonzeroFloat64), the result is (0, Below) or (-0, Above), respectively, depending on the sign of f. If f is too large to be represented by a float64 (|f| > math.MaxFloat64), the result is (+Inf, Above) or (-Inf, Below), depending on the sign of f.
type HexOrDecimal256 ¶
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.