gnum

package
v0.0.0-...-bad6181 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2024 License: GPL-3.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Billion = 1000000000
	Million = 1000000
	Kilo    = 1000
)
View Source
const (
	GoldenRatio = 0.618
)
View Source
const (
	// MaxUint24 maximum value of uint24 variable
	MaxUint24 = 1<<24 - 1
)

Variables

View Source
var (
	Decimal0 = Decimal(decimal.Zero)
	Decimal1 = NewDecimalFromInt(1)
)
View Source
var (
	PosInf = math.Inf(1)
	NegInf = math.Inf(-1)
)

Functions

func AddSS

func AddSS(inReal0, inReal1 []float64) []float64

func AddSV

func AddSV(inReal []float64, v float64) []float64

func Avg

func Avg(inReal []float64, timePeriod int) []float64

Avg - Vector average

func BaseConvert

func BaseConvert(num string, fromBase, toBase int) (string, error)

func BigIntToFloat64

func BigIntToFloat64(it *big.Int) float64

func BiggerEqualThan

func BiggerEqualThan(left, right *big.Int) bool

func BiggerThan

func BiggerThan(left, right *big.Int) bool

func BoundUint32

func BoundUint32(min, toBound, max uint32) uint32

给toBound划界,不超过[min, max]的范围

func CmpInt64Array

func CmpInt64Array(a, b []int64) bool

func ContainsInt

func ContainsInt(src []int, toFind int) bool

func DetectMaxPrec

func DetectMaxPrec(vals []ElegantFloat, humanReadPrec int) int

func DetectMaxPrecRaw

func DetectMaxPrecRaw(vals []float64, humanReadPrec int) int

func DetectPrecByHumanReadPrec

func DetectPrecByHumanReadPrec(val float64, humanReadPrec int) int

func ElegantFloatArrayToFloatArray

func ElegantFloatArrayToFloatArray(in []ElegantFloat) []float64

func Fibonacci

func Fibonacci(n int) int

func Float64ToByte

func Float64ToByte(f float64) []byte

Converts the float64 into an uint64 without changing the bits, it's the way the bits are interpreted that change. big endian references: https://stackoverflow.com/questions/37758267/golang-float64bits https://stackoverflow.com/questions/43693360/convert-float64-to-byte-array

func Float64ToByteLE

func Float64ToByteLE(f float64) []byte

little endian

func FloatAlmostEqual

func FloatAlmostEqual(a, b float64) bool

func FloatsEqual

func FloatsEqual(a, b []float64) bool

func FloatsPtrToFloats

func FloatsPtrToFloats(in []*float64) []float64

func FloatsToFloatsPtr

func FloatsToFloatsPtr(in []float64) []*float64

func Format

func Format(num any, base int) (string, error)

func FormatAnySys

func FormatAnySys(numberSystemDigits []rune, n big.Int) string

Format number to string in specified number system. Length of 'numberSystemDigits' is number system length. Reference: https://github.com/kenticny/numconvert/blob/master/converter.go

func FormatFloat64

func FormatFloat64(f float64, prec int) string

func FormatInt

func FormatInt(i int) string

func FormatInt64

func FormatInt64(i int64) string

func FormatUint16

func FormatUint16(u uint16) string

func FormatUint32

func FormatUint32(u uint32) string

func FormatUint64

func FormatUint64(u uint64) string

func FormatUint8

func FormatUint8(u uint8) string

func GetDefaultDigits

func GetDefaultDigits() []rune

func GetLast1

func GetLast1(v int) int

199012 -> 2

func GetLast2

func GetLast2(v int) int

199012 -> 12

func IsDigit

func IsDigit(s string) bool

123 00123 +123 -123 123.456 2.07539829195e-05

func IsEvenInt64

func IsEvenInt64(n int64) bool

func IsEvenUint64

func IsEvenUint64(n uint64) bool

func IsOddInt64

func IsOddInt64(n int64) bool

func IsOddUint64

func IsOddUint64(n uint64) bool

func IsZero

func IsZero(val *big.Int) bool

func JoinFormatInt

func JoinFormatInt(sep string, num ...int) string

func Max

func Max(numTypeSample any) (any, error)

func MaxFloat

func MaxFloat(args ...float64) float64

func MaxFloat64

func MaxFloat64(first float64, args ...float64) float64

func MaxInt

func MaxInt(first int, args ...int) int

func MaxInt64

func MaxInt64(first int64, args ...int64) int64

func MaxPrec

func MaxPrec(in []Decimal) int

func MaxSS

func MaxSS(inReal0, inReal1 []float64) []float64

func MaxSV

func MaxSV(inReal []float64, inTimePeriod int) []float64

func MaxUint32

func MaxUint32(first uint32, args ...uint32) uint32

func MaxUint64

func MaxUint64(first uint64, args ...uint64) uint64

func Mean

func Mean(values []float64) float64

Mean (均值)

func Min

func Min(numTypeSample any) (any, error)

func MinFloat

func MinFloat(args ...float64) float64

func MinFloat64

func MinFloat64(first float64, args ...float64) float64

func MinInt

func MinInt(first int, args ...int) int

func MinInt64

func MinInt64(first int64, args ...int64) int64

func MinSS

func MinSS(inReal0, inReal1 []float64) []float64

func MinSV

func MinSV(inReal []float64, inTimePeriod int) []float64

func MinUint32

func MinUint32(first uint32, args ...uint32) uint32

func MinUint64

func MinUint64(first uint64, args ...uint64) uint64

func MinUintArray

func MinUintArray(args []uint) uint

func MulSS

func MulSS(inReal0, inReal1 []float64) []float64

func MulSV

func MulSV(inReal []float64, v float64) []float64

func MustMax

func MustMax(numTypeSample any) any

func MustMin

func MustMin(numTypeSample any) any

func ParseAnySys

func ParseAnySys(numberSystemDigits []rune, s string) (*big.Int, error)

Parse string as specified number system.

func ParseBigInt

func ParseBigInt(num string, base int) (big.Int, error)

func ParseFloat64

func ParseFloat64(s string) (float64, error)

func RealGoldenRatio

func RealGoldenRatio() float64

func RemoveDuplicate

func RemoveDuplicate(elements []int) []int

func RemoveLast1

func RemoveLast1(v int) int

199012 -> 19901

func RemoveLast2

func RemoveLast2(v int) int

199012 -> 1990

func RemoveUint

func RemoveUint(src []uint, toRemove uint, removeCount int) []uint

func ReverseBigFloats

func ReverseBigFloats(in []big.Float)

func ReverseBigInts

func ReverseBigInts(in []big.Int)

func ReverseFloats

func ReverseFloats(in []float64)

func ReverseInt64s

func ReverseInt64s(in []int64)

func ReverseInts

func ReverseInts(in []int)

func ReverseUints

func ReverseUints(in []uint)

func SetHead0

func SetHead0(inReal []float64, count int)

func Slice

func Slice(v int64, begin, len int) (int64, error)

SliceBetweenEqual(19901201, 4, 2) -> 12 SliceBetweenEqual(-19901201, 4, 2) -> 12

func SmallerEqualThan

func SmallerEqualThan(left, right *big.Int) bool

func SmallerThan

func SmallerThan(left, right *big.Int) bool

func SortBigFloats

func SortBigFloats(in []big.Float)

func SortBigInts

func SortBigInts(in []big.Int)

func SortFloats

func SortFloats(in []float64)

func SortInt64s

func SortInt64s(in []int64)

func SortInts

func SortInts(in []int)

func SortUints

func SortUints(in []uint)

func Std

func Std(values []float64, ddof int) float64

Standard Deviation (均方差) ddof: Delta Degrees of Freedom. The divisor used in calculations is N - ddof, where N represents the number of elements. By default ddof is zero.

func SubSS

func SubSS(inReal0, inReal1 []float64) []float64

func SubSV

func SubSV(inReal []float64, v float64) []float64

func Sum

func Sum(values []float64) float64

Sum (和)

func SumFloat

func SumFloat(args ...float64) float64

func ToFloat64s

func ToFloat64s(in []Decimal) []float64

Types

type Decimal

type Decimal decimal.Decimal

func MaxDecimal

func MaxDecimal(first Decimal, args ...Decimal) Decimal

func MinDecimal

func MinDecimal(first Decimal, args ...Decimal) Decimal

func NewDecimalFromAny

func NewDecimalFromAny(val any) (*Decimal, error)

func NewDecimalFromBigFloat

func NewDecimalFromBigFloat(val big.Float) (Decimal, error)

func NewDecimalFromBigInt

func NewDecimalFromBigInt(val big.Int) Decimal

TODO: test required

func NewDecimalFromDecimal

func NewDecimalFromDecimal(d2 decimal.Decimal) Decimal

func NewDecimalFromFloat32

func NewDecimalFromFloat32(val float32) Decimal

func NewDecimalFromFloat64

func NewDecimalFromFloat64(val float64) Decimal

func NewDecimalFromInt

func NewDecimalFromInt(val int) Decimal

func NewDecimalFromInt16

func NewDecimalFromInt16(val int16) Decimal

func NewDecimalFromInt32

func NewDecimalFromInt32(val int32) Decimal

func NewDecimalFromInt64

func NewDecimalFromInt64(val int64) Decimal

func NewDecimalFromInt8

func NewDecimalFromInt8(val int8) Decimal

func NewDecimalFromString

func NewDecimalFromString(val string) (Decimal, error)

func NewDecimalFromStringEx

func NewDecimalFromStringEx(val string, prec int) (Decimal, error)

func NewDecimalFromUint

func NewDecimalFromUint(val uint) Decimal

func NewDecimalFromUint16

func NewDecimalFromUint16(val uint16) Decimal

func NewDecimalFromUint32

func NewDecimalFromUint32(val uint32) Decimal

func NewDecimalFromUint64

func NewDecimalFromUint64(val uint64) Decimal

func NewDecimalFromUint8

func NewDecimalFromUint8(val uint8) Decimal

func (Decimal) Abs

func (d Decimal) Abs() Decimal

func (Decimal) Add

func (d Decimal) Add(d2 Decimal) Decimal

func (Decimal) AddFloat64

func (d Decimal) AddFloat64(n float64) Decimal

func (Decimal) AddInt

func (d Decimal) AddInt(n int) Decimal

func (Decimal) BitsAfterDecimalPoint

func (d Decimal) BitsAfterDecimalPoint(countTail0 bool) int

BitsAfterDecimalPoint returns decimal precision. NOTE: When countTail0 is true, the 0 in the tail is also counted as precision.

func (*Decimal) Conv

func (d *Decimal) Conv(sample any, allowFractionalLoss bool) (any, error)

Conv converts decimal to a number which has same type with `sample`.

func (Decimal) Div

func (d Decimal) Div(d2 Decimal) Decimal

func (Decimal) DivFloat64

func (d Decimal) DivFloat64(n float64) Decimal

func (Decimal) DivInt

func (d Decimal) DivInt(n int) Decimal

func (Decimal) DivRound

func (d Decimal) DivRound(d2 Decimal, precision int) Decimal

FIXME: 这个是RoundUp还是RoundDown?

func (Decimal) DivRoundDown

func (d Decimal) DivRoundDown(d2 Decimal, precision int) Decimal

func (Decimal) DivRoundFloat64

func (d Decimal) DivRoundFloat64(d2 float64, precision int) Decimal

func (Decimal) DivRoundInt

func (d Decimal) DivRoundInt(d2 int, precision int) Decimal

func (Decimal) Equal

func (d Decimal) Equal(cmp Decimal) bool

func (Decimal) EqualFloat64

func (d Decimal) EqualFloat64(cmp float64) bool

func (Decimal) EqualInt

func (d Decimal) EqualInt(cmp int) bool

func (Decimal) Float64

func (d Decimal) Float64() float64

func (Decimal) Float64Ex

func (d Decimal) Float64Ex() (val float64, exact bool)

func (Decimal) GreaterThan

func (d Decimal) GreaterThan(cmp Decimal) bool

func (Decimal) GreaterThanFloat64

func (d Decimal) GreaterThanFloat64(cmp float64) bool

func (Decimal) GreaterThanInt

func (d Decimal) GreaterThanInt(cmp int) bool

func (Decimal) GreaterThanOrEqual

func (d Decimal) GreaterThanOrEqual(cmp Decimal) bool

func (Decimal) GreaterThanOrEqualFloat64

func (d Decimal) GreaterThanOrEqualFloat64(cmp float64) bool

func (Decimal) GreaterThanOrEqualInt

func (d Decimal) GreaterThanOrEqualInt(cmp int) bool

func (Decimal) Int64Part

func (d Decimal) Int64Part() int64

func (Decimal) IntPart

func (d Decimal) IntPart() int

func (Decimal) IsInterger

func (d Decimal) IsInterger() bool

func (Decimal) IsNegative

func (d Decimal) IsNegative() bool

func (Decimal) IsPositive

func (d Decimal) IsPositive() bool

func (Decimal) IsZero

func (d Decimal) IsZero() bool

func (Decimal) LessThan

func (d Decimal) LessThan(cmp Decimal) bool

func (Decimal) LessThanFloat64

func (d Decimal) LessThanFloat64(cmp float64) bool

func (Decimal) LessThanInt

func (d Decimal) LessThanInt(cmp int) bool

func (Decimal) LessThanOrEqual

func (d Decimal) LessThanOrEqual(cmp Decimal) bool

func (Decimal) LessThanOrEqualFloat64

func (d Decimal) LessThanOrEqualFloat64(cmp float64) bool

func (Decimal) LessThanOrEqualInt

func (d Decimal) LessThanOrEqualInt(cmp int) bool

func (Decimal) MarshalBSONValue

func (d Decimal) MarshalBSONValue() (bsontype.Type, []byte, error)

in BSON, Decimal128 could has 34 numbers after decimal point, it will loss precision if more than 34 numbers after decimal point so we serialize Decimal into string, just like in JSON. in BSON, omitempty of Decimal works reference: https://github.com/hackeryard/configcenter/blob/a9638f554b4cf47fb13c37c5c3611e1e0696fd25/src/common/metadata/time.go#L94

func (Decimal) MarshalBinary

func (d Decimal) MarshalBinary() (data []byte, err error)

func (Decimal) MarshalJSON

func (d Decimal) MarshalJSON() ([]byte, error)

NOTE: if omitempty required in JSON, use *Decimal in your structure, no other way to implement this even you change source code MarshalJSON function in shopsprint/decimal

func (Decimal) MarshalJSONWithoutQuotes

func (d Decimal) MarshalJSONWithoutQuotes() ([]byte, error)

WARNING: this is dangerous for decimals with many digits, since many JSON unmarshallers (ex: Javascript's) will unmarshal JSON numbers to IEEE 754 double-precision floating point numbers, which means you can potentially silently lose precision.

func (Decimal) MarshalText

func (d Decimal) MarshalText() (text []byte, err error)

func (Decimal) Mul

func (d Decimal) Mul(d2 Decimal) Decimal

func (Decimal) MulFloat64

func (d Decimal) MulFloat64(n float64) Decimal

func (Decimal) MulInt

func (d Decimal) MulInt(n int) Decimal

func (*Decimal) SetFloat64

func (d *Decimal) SetFloat64(val float64)

func (*Decimal) SetInt

func (d *Decimal) SetInt(val int)

func (Decimal) String

func (d Decimal) String() string

func (Decimal) Sub

func (d Decimal) Sub(d2 Decimal) Decimal

func (Decimal) SubFloat64

func (d Decimal) SubFloat64(n float64) Decimal

func (Decimal) SubInt

func (d Decimal) SubInt(n int) Decimal

func (*Decimal) ToBSONDecimal128

func (d *Decimal) ToBSONDecimal128() (primitive.Decimal128, error)

WARN: BSON Decimal128 has max 34 number after "." but Decimal doesn't have this limit (limited by your memory)

func (*Decimal) ToElegantFloat

func (d *Decimal) ToElegantFloat() ElegantFloat

func (Decimal) Trunc

func (d Decimal) Trunc(prec int, step float64) Decimal

prec就是小数点后最多支持多少位

func (Decimal) Trunc2

func (d Decimal) Trunc2(min Decimal, step float64) Decimal

min 就是小数点后最多多少位的小数写法,比如,min=0.00001,prec就是5

func (Decimal) TurnPositiveNegative

func (d Decimal) TurnPositiveNegative() Decimal

func (*Decimal) UnmarshalBSONValue

func (d *Decimal) UnmarshalBSONValue(t bsontype.Type, data []byte) error

func (*Decimal) UnmarshalBinary

func (d *Decimal) UnmarshalBinary(b []byte) error

func (*Decimal) UnmarshalJSON

func (d *Decimal) UnmarshalJSON(b []byte) error

func (*Decimal) UnmarshalText

func (d *Decimal) UnmarshalText(text []byte) error

func (Decimal) WithPrec

func (d Decimal) WithPrec(prec int) Decimal

type ElegantFloat

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

original -> prec: 2 -> humanReadPrec: 2 37 -> 37.00 -> 37.00 12237.89374 -> 12237.89 -> 12237.89 3.3483300000000003 -> 3.35 -> 3.35 0.00883300000000003 -> 0.01 -> 0.0088 0.000012800003 -> 0.00 -> 0.000013

func NewElegantFloat

func NewElegantFloat(val float64, prec int) ElegantFloat

func NewElegantFloatArray

func NewElegantFloatArray(vals []float64, prec int) []ElegantFloat

func NewElegantFloatPtrArray

func NewElegantFloatPtrArray(vals []*float64, prec int) []*ElegantFloat

func NewElegantFloatPtrArray2

func NewElegantFloatPtrArray2(vals []float64, prec int) []*ElegantFloat

func NewElegantFloatPtrArray3

func NewElegantFloatPtrArray3(vals []float64, prec int, nilVal float64) []*ElegantFloat

func ToElegantFloat64s

func ToElegantFloat64s(in []Decimal) []ElegantFloat

func (*ElegantFloat) JSON

func (t *ElegantFloat) JSON(fmtNaNasNull bool) ([]byte, error)

What will happen if value is math.NaN? it will output bytes buffer `"NaN"`

func (ElegantFloat) MarshalJSON

func (t ElegantFloat) MarshalJSON() ([]byte, error)

MarshalJSON will marshal using 2006-01-02T15:04:05+07:00 layout

FIXME if t.val = math.NaN and t.JSON(false), json.Marshal(t) will output error: json: error calling MarshalJSON for type *gnum.ElegantFloat: invalid character 'N' looking for beginning of value why and how to fix?

func (*ElegantFloat) Raw

func (t *ElegantFloat) Raw() float64

func (*ElegantFloat) SetHumanReadPrec

func (t *ElegantFloat) SetHumanReadPrec(humanReadPrec int)

func (*ElegantFloat) SetPrec

func (t *ElegantFloat) SetPrec(prec int)

func (*ElegantFloat) String

func (t *ElegantFloat) String() string

func (*ElegantFloat) UnmarshalJSON

func (t *ElegantFloat) UnmarshalJSON(b []byte) error

TODO: NaN +-Inf会被ParseFloat64认为是错误吗,需要测试一下 UnmarshalJSON will unmarshal using 2006-01-02T15:04:05+07:00 layout

type Uint24

type Uint24 [3]uint8

Uint24 type

func NewUint24

func NewUint24(val uint32) Uint24

NewUint24 creates a new uint24 type

func NewUint24FromBytes

func NewUint24FromBytes(b [3]byte) Uint24

func (Uint24) String

func (u Uint24) String() string

String converts uint24 to string

func (Uint24) ToBytes

func (u Uint24) ToBytes() []byte

ToBytes converts uint24 to bytes array

func (Uint24) Uint32

func (u Uint24) Uint32() uint32

Uint32 converts uint24 to uint32

Jump to

Keyboard shortcuts

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