mathext

package
v1.0.12 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2025 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs[T Number](x T) T

func Dim

func Dim[T Number](x, y T) T

func F32to64

func F32to64(f uint32) uint64

func F32toint32

func F32toint32(x uint32) int32

func F32toint64

func F32toint64(x uint32) int64

func F32touint64

func F32touint64(x float32) uint64

func F64to32

func F64to32(f uint64) uint32

func F64toInt

func F64toInt(f uint64) (val int64, ok bool)

func F64toint32

func F64toint32(x uint64) int32

func F64toint64

func F64toint64(x uint64) int64

func F64touint64

func F64touint64(x float64) uint64

func Fadd32

func Fadd32(x, y uint32) uint32

func Fadd64

func Fadd64(f, g uint64) uint64

func Fcmp64

func Fcmp64(f, g uint64) (cmp int32, isnan bool)

func Fdiv32

func Fdiv32(x, y uint32) uint32

func Fdiv64

func Fdiv64(f, g uint64) uint64

func Feq32

func Feq32(x, y uint32) bool

func Feq64

func Feq64(x, y uint64) bool

func Fge32

func Fge32(x, y uint32) bool

func Fge64

func Fge64(x, y uint64) bool

func Fgt32

func Fgt32(x, y uint32) bool

func Fgt64

func Fgt64(x, y uint64) bool

func Fint32to32

func Fint32to32(x int32) uint32

func Fint32to64

func Fint32to64(x int32) uint64

func Fint64to32

func Fint64to32(x int64) uint32

func Fint64to64

func Fint64to64(x int64) uint64

func Fintto64

func Fintto64(val int64) (f uint64)

func Fmul32

func Fmul32(x, y uint32) uint32

func Fmul64

func Fmul64(f, g uint64) uint64

func Fneg64

func Fneg64(f uint64) uint64

func Fpack32

func Fpack32(sign, mant uint32, exp int, trunc uint32) uint32

func Fpack64

func Fpack64(sign, mant uint64, exp int, trunc uint64) uint64

func Fsub64

func Fsub64(f, g uint64) uint64

func Fuint64to32

func Fuint64to32(x uint64) float32

func Fuint64to64

func Fuint64to64(x uint64) float64

func Funpack32

func Funpack32(f uint32) (sign, mant uint32, exp int, inf, nan bool)

func Funpack64

func Funpack64(f uint64) (sign, mant uint64, exp int, inf, nan bool)

func IsAlmostEqualFloat32

func IsAlmostEqualFloat32(a, b float32) bool

func IsAlmostEqualFloat64

func IsAlmostEqualFloat64(a, b float64) bool

IsAlmostEqualFloat64 returns true if this number is at most kMaxUlps ULP's away from rhs. In particular, this function:

  • returns false if either number is (or both are) NAN.
  • treats really large numbers as almost equal to infinity.
  • thinks +0.0 and -0.0 are 0 DLP's apart.

func Max

func Max[T Number](x, y T) T

func Min

func Min[T Number](x, y T) T

func RandFloat

func RandFloat(min, max float64) float64

RandFloat rand a float number in [min, max]

func RandInt

func RandInt(min, max int) int

RandInt rand an integer in [min, max]

func RangePerm

func RangePerm(min, max int) []int

RangePerm [min,max]区间内的随机数集合

func RoundFloat

func RoundFloat(x float64, places int) float64

RoundFloat round a float to a specific decimal place or precision see https://github.com/montanaflynn/stats/blob/master/round.go

func RoundHalf

func RoundHalf(v float64) int

RoundHalf 四舍五入

func SafeDiv

func SafeDiv[T Number](x, y T) T

func SafeMulInt64

func SafeMulInt64(a, b int64) (product int64, overflow bool)

func SafeMulUint64

func SafeMulUint64(a, b uint64) (product uint64, overflow bool)

func SignAndMagnitudeToBiasedFloat32

func SignAndMagnitudeToBiasedFloat32(sam uint32) uint32

func SignAndMagnitudeToBiasedFloat64

func SignAndMagnitudeToBiasedFloat64(sam uint64) uint64

SignAndMagnitudeToBiasedFloat64 converts an integer from the sign-and-magnitude representation to the biased representation. More precisely, let N be 2 to the power of (kBitCount - 1), an integer x is represented by the unsigned number x + N.

For instance,

-N + 1 (the most negative number representable using
       sign-and-magnitude) is represented by 1;
0      is represented by N; and
N - 1  (the biggest number representable using
       sign-and-magnitude) is represented by 2N - 1.

Read http://en.wikipedia.org/wiki/Signed_number_representations for more details on signed number representations.

func Truncate

func Truncate(f float64, n int) float64

Truncate 截断浮点数的`n`位后,n不应过大

Types

type Float

type Float interface {
	~float32 | ~float64
}

type Integer

type Integer interface {
	Signed | Unsigned
}

type LCGRand32 added in v1.0.11

type LCGRand32 uint32

func (*LCGRand32) Int63 added in v1.0.11

func (g *LCGRand32) Int63() int64

func (*LCGRand32) Seed added in v1.0.11

func (g *LCGRand32) Seed(seed uint32)

func (*LCGRand32) Uint32 added in v1.0.11

func (g *LCGRand32) Uint32() uint32

func (*LCGRand32) Uint64 added in v1.0.11

func (g *LCGRand32) Uint64() uint64

type LCGRand64 added in v1.0.11

type LCGRand64 uint64

func (*LCGRand64) Int63 added in v1.0.11

func (g *LCGRand64) Int63() int64

func (*LCGRand64) Seed added in v1.0.11

func (g *LCGRand64) Seed(seed uint64)

func (*LCGRand64) Uint64 added in v1.0.11

func (g *LCGRand64) Uint64() uint64

type Number

type Number interface {
	Integer | Float
}

type Signed

type Signed interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

type Uint128

type Uint128 struct {
	Lo, Hi uint64
}

An Uint128 is an unsigned 128-bit number.

var (
	Uint128Zero Uint128
	Uint128Max  = NewUint128(math.MaxUint64, math.MaxUint64)
)

func NewUint128

func NewUint128(lo, hi uint64) Uint128

NewUint128 returns the Uint128 value (lo,hi).

func Uint128From64

func Uint128From64(v uint64) Uint128

Uint128From64 converts v to a Uint128 value.

func Uint128FromBig

func Uint128FromBig(i *big.Int) (u Uint128)

Uint128FromBig converts i to a Uint128 value. It panics if i is negative or overflows 128 bits.

func Uint128FromBytes

func Uint128FromBytes(b []byte) Uint128

Uint128FromBytes converts b to a Uint128 value.

func Uint128FromBytesBE

func Uint128FromBytesBE(b []byte) Uint128

Uint128FromBytesBE converts big-endian b to a Uint128 value.

func Uint128FromString

func Uint128FromString(s string) (u Uint128, err error)

Uint128FromString parses s as a Uint128 value.

func (Uint128) Add

func (u Uint128) Add(v Uint128) Uint128

Add returns u+v.

func (Uint128) Add64

func (u Uint128) Add64(v uint64) Uint128

Add64 returns u+v.

func (Uint128) AddWrap

func (u Uint128) AddWrap(v Uint128) Uint128

AddWrap returns u+v with wraparound semantics; for example, Max.AddWrap(Uint128From64(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) Big

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

Big returns u as a *big.Int.

func (Uint128) Compare64

func (u Uint128) Compare64(v uint64) int

Compare64 compares u and v and returns:

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

func (Uint128) CompareTo

func (u Uint128) CompareTo(v Uint128) int

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

func (Uint128) Div64

func (u Uint128) Div64(v uint64) Uint128

Div64 returns u/v.

func (Uint128) Equals

func (u Uint128) Equals(v Uint128) bool

Equals returns true if u == v.

func (Uint128) Equals64

func (u Uint128) Equals64(v uint64) bool

Equals64 returns true if u == v.

func (Uint128) IsZero

func (u Uint128) IsZero() bool

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

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

Mod returns r = u%v.

func (Uint128) Mod64

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

Mod64 returns r = u%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) PutBytes

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

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

func (Uint128) PutBytesBE

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

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

func (Uint128) QuoRem

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

QuoRem returns q = u/v and r = u%v.

func (Uint128) QuoRem64

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

QuoRem64 returns q = u/v and r = u%v.

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

func (u *Uint128) Scan(s fmt.ScanState, ch rune) error

Scan implements fmt.Scanner.

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.

func (Uint128) Sub64

func (u Uint128) Sub64(v uint64) Uint128

Sub64 returns u-v.

func (Uint128) SubWrap

func (u Uint128) SubWrap(v Uint128) Uint128

SubWrap returns u-v with wraparound semantics; for example, Zero.SubWrap(Uint128From64(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) 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.

type Unsigned

type Unsigned interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

type WyRand added in v1.0.11

type WyRand uint64

WyRand see https://github.com/wangyi-fudan/wyhash

func (*WyRand) Uint32 added in v1.0.11

func (r *WyRand) Uint32() uint32

func (*WyRand) Uint32n added in v1.0.11

func (r *WyRand) Uint32n(n int) uint32

func (*WyRand) Uint64 added in v1.0.11

func (r *WyRand) Uint64() uint64

func (*WyRand) Uint64n added in v1.0.11

func (r *WyRand) Uint64n(n uint64) uint64

Jump to

Keyboard shortcuts

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