mathutil

package
v0.6.15 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2024 License: MIT Imports: 10 Imported by: 28

README

Math Utils

  • some features

Install

go get github.com/gookit/goutil/mathutil

Go docs

Usage

Functions


func CompFloat[T comdef.Float](first, second T, op string) (ok bool)
func CompInt[T comdef.Xint](first, second T, op string) (ok bool)
func CompInt64(first, second int64, op string) bool
func CompValue[T comdef.XintOrFloat](first, second T, op string) (ok bool)
func Compare(first, second any, op string) (ok bool)
func DataSize(size uint64) string
func ElapsedTime(startTime time.Time) string
func Float(in any) (float64, error)
func FloatOr(in any, defVal float64) float64
func FloatOrDefault(in any, defVal float64) float64
func FloatOrErr(in any) (float64, error)
func FloatOrPanic(in any) float64
func GreaterOr[T comdef.XintOrFloat](val, min, defVal T) T
func GteOr[T comdef.XintOrFloat](val, min, defVal T) T
func HowLongAgo(sec int64) string
func InRange[T comdef.IntOrFloat](val, min, max T) bool
func InUintRange[T comdef.Uint](val, min, max T) bool
func Int(in any) (int, error)
func Int64(in any) (int64, error)
func Int64OrErr(in any) (int64, error)
func IntOr(in any, defVal int) int
func IntOrDefault(in any, defVal int) int
func IntOrErr(in any) (iVal int, err error)
func IntOrPanic(in any) int
func IsNumeric(c byte) bool
func LessOr[T comdef.XintOrFloat](val, max, devVal T) T
func LteOr[T comdef.XintOrFloat](val, max, devVal T) T
func Max[T comdef.XintOrFloat](x, y T) T
func MaxFloat(x, y float64) float64
func MaxI64(x, y int64) int64
func MaxInt(x, y int) int
func Min[T comdef.XintOrFloat](x, y T) T
func MustFloat(in any) float64
func MustInt(in any) int
func MustInt64(in any) int64
func MustString(val any) string
func MustUint(in any) uint64
func OrElse[T comdef.XintOrFloat](val, defVal T) T
func OutRange[T comdef.IntOrFloat](val, min, max T) bool
func Percent(val, total int) float64
func QuietFloat(in any) float64
func QuietInt(in any) int
func QuietInt64(in any) int64
func QuietString(val any) string
func QuietUint(in any) uint64
func RandInt(min, max int) int
func RandIntWithSeed(min, max int, seed int64) int
func RandomInt(min, max int) int
func RandomIntWithSeed(min, max int, seed int64) int
func SafeFloat(in any) float64
func SafeInt(in any) int
func SafeInt64(in any) int64
func SafeUint(in any) uint64
func StrInt(s string) int
func StrIntOr(s string, defVal int) int
func String(val any) string
func StringOrErr(val any) (string, error)
func StringOrPanic(val any) string
func SwapMax[T comdef.XintOrFloat](x, y T) (T, T)
func SwapMaxI64(x, y int64) (int64, int64)
func SwapMaxInt(x, y int) (int, int)
func SwapMin[T comdef.XintOrFloat](x, y T) (T, T)
func ToFloat(in any) (f64 float64, err error)
func ToFloatWithFunc(in any, usrFn func(any) (float64, error)) (f64 float64, err error)
func ToInt(in any) (iVal int, err error)
func ToInt64(in any) (i64 int64, err error)
func ToString(val any) (string, error)
func ToUint(in any) (u64 uint64, err error)
func ToUintWithFunc(in any, usrFn func(any) (uint64, error)) (u64 uint64, err error)
func TryToString(val any, defaultAsErr bool) (str string, err error)
func Uint(in any) (uint64, error)
func UintOrErr(in any) (uint64, error)
func ZeroOr[T comdef.XintOrFloat](val, defVal T) T

Testings

go test -v ./mathutil/...

Test limit by regexp:

go test -v -run ^TestSetByKeys ./mathutil/...

Documentation

Overview

Package mathutil provide math(int, number) util functions. eg: convert, math calc, random

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CompFloat added in v0.5.8

func CompFloat[T comdef.Float](first, second T, op string) (ok bool)

CompFloat compare float64,float32 value. returns `first op(=,!=,<,<=,>,>=) second`

func CompInt added in v0.6.9

func CompInt[T comdef.Xint](first, second T, op string) (ok bool)

CompInt compare all intX,uintX type value. returns `first op(=,!=,<,<=,>,>=) second`

func CompInt64 added in v0.5.8

func CompInt64(first, second int64, op string) bool

CompInt64 compare int64 value. returns `first op(=,!=,<,<=,>,>=) second`

func CompValue added in v0.6.9

func CompValue[T comdef.XintOrFloat](first, second T, op string) (ok bool)

CompValue compare intX,uintX,floatX value. returns `first op(=,!=,<,<=,>,>=) second`

func Compare added in v0.5.8

func Compare(first, second any, op string) bool

Compare any intX,floatX value by given op. returns `first op(=,!=,<,<=,>,>=) second`

Usage:

mathutil.Compare(2, 3, ">") // false
mathutil.Compare(2, 1.3, ">") // true
mathutil.Compare(2.2, 1.3, ">") // true
mathutil.Compare(2.1, 2, ">") // true

func DataSize

func DataSize(size uint64) string

DataSize format bytes number friendly. eg: 1024 => 1KB, 1024*1024 => 1MB

Usage:

file, err := os.Open(path)
fl, err := file.Stat()
fmtSize := DataSize(fl.Size())

func Div added in v0.6.15

func Div[T1, T2 comdef.XintOrFloat](a T1, b T2) float64

Div computes the a/b value, result use round handle.

func DivF2i added in v0.6.15

func DivF2i(a, b float64) int

DivF2i computes the float64 type a / b value, rounding the result to an integer.

func DivInt added in v0.6.15

func DivInt[T comdef.Integer](a, b T) int

DivInt computes the int type a / b value, rounding the result to an integer.

func Float added in v0.2.2

func Float(in any) (float64, error)

Float convert value to float64, return error on failed

func FloatOr added in v0.6.12

func FloatOr(in any, defVal float64) float64

FloatOr convert value to float64, will return default value on error

func FloatOrDefault added in v0.6.12

func FloatOrDefault(in any, defVal float64) float64

FloatOrDefault convert value to float64, will return default value on error

func FloatOrErr added in v0.5.3

func FloatOrErr(in any) (float64, error)

FloatOrErr convert value to float64, return error on failed

func FloatOrPanic added in v0.5.0

func FloatOrPanic(in any) float64

FloatOrPanic convert value to float64, will panic on error

func GreaterOr added in v0.6.12

func GreaterOr[T comdef.XintOrFloat](val, min, defVal T) T

GreaterOr return val on val > max, else return default value.

Example:

GreaterOr(23, 0, 2) // 23
GreaterOr(0, 0, 2) // 2

func GteOr added in v0.6.12

func GteOr[T comdef.XintOrFloat](val, min, defVal T) T

GteOr return val on val >= max, else return default value.

Example:

GteOr(23, 0, 2) // 23
GteOr(0, 0, 2) // 0

func HowLongAgo

func HowLongAgo(sec int64) string

HowLongAgo format a seconds, get how lang ago. eg: 1 day, 1 week

func InRange added in v0.6.9

func InRange[T comdef.IntOrFloat](val, min, max T) bool

InRange check if val in int/float range [min, max]

func InUintRange added in v0.6.9

func InUintRange[T comdef.Uint](val, min, max T) bool

InUintRange check if val in unit range [min, max]

func Int added in v0.2.2

func Int(in any) (int, error)

Int convert value to int

func Int64 added in v0.2.2

func Int64(in any) (int64, error)

Int64 convert value to int64, return error on failed

func Int64Or added in v0.6.12

func Int64Or(in any, defVal int64) int64

Int64Or convert value to int64, return default val on failed

func Int64OrDefault added in v0.6.12

func Int64OrDefault(in any, defVal int64) int64

Int64OrDefault convert value to int64, return default val on failed

func Int64OrErr added in v0.5.3

func Int64OrErr(in any) (int64, error)

Int64OrErr convert value to int64, return error on failed

func IntOr added in v0.6.12

func IntOr(in any, defVal int) int

IntOr convert value to int, return defaultVal on failed

func IntOrDefault added in v0.6.12

func IntOrDefault(in any, defVal int) int

IntOrDefault convert value to int, return defaultVal on failed

func IntOrErr added in v0.5.3

func IntOrErr(in any) (int, error)

IntOrErr convert value to int, return error on failed

func IntOrPanic added in v0.4.1

func IntOrPanic(in any) int

IntOrPanic convert value to int, will panic on error

func IsNumeric added in v0.3.12

func IsNumeric(c byte) bool

IsNumeric returns true if the given character is a numeric, otherwise false.

func LessOr added in v0.6.12

func LessOr[T comdef.XintOrFloat](val, max, devVal T) T

LessOr return val on val < max, else return default value.

Example:

LessOr(11, 10, 1) // 1
LessOr(2, 10, 1) // 2
LessOr(10, 10, 1) // 1

func LteOr added in v0.6.12

func LteOr[T comdef.XintOrFloat](val, max, devVal T) T

LteOr return val on val <= max, else return default value.

Example:

LteOr(11, 10, 1) // 11
LteOr(2, 10, 1) // 2
LteOr(10, 10, 1) // 10

func Max added in v0.6.2

func Max[T comdef.XintOrFloat](x, y T) T

Max compare two value and return max value

func MaxFloat added in v0.5.3

func MaxFloat(x, y float64) float64

MaxFloat compare and return max value

func MaxI64 added in v0.5.3

func MaxI64(x, y int64) int64

MaxI64 compare and return max value

func MaxInt added in v0.5.3

func MaxInt(x, y int) int

MaxInt compare and return max value

func Min added in v0.6.8

func Min[T comdef.XintOrFloat](x, y T) T

Min compare two value and return max value

func Mul added in v0.6.15

func Mul[T1, T2 comdef.XintOrFloat](a T1, b T2) float64

Mul computes the a*b value, rounding the result.

func MulF2i added in v0.6.15

func MulF2i(a, b float64) int

MulF2i computes the float64 type a * b value, rounding the result to an integer.

func MustFloat added in v0.2.2

func MustFloat(in any) float64

MustFloat convert value to float64, will panic on error

func MustInt added in v0.2.2

func MustInt(in any) int

MustInt convert value to int, will panic on error

func MustInt64 added in v0.2.2

func MustInt64(in any) int64

MustInt64 convert value to int64, will panic on error

func MustString added in v0.5.0

func MustString(val any) string

MustString convert intX/floatX value to string, will panic on error

func MustUint added in v0.2.2

func MustUint(in any) uint

MustUint convert any to uint, will panic on error

func MustUint64 added in v0.6.15

func MustUint64(in any) uint64

MustUint64 convert any to uint64, will panic on error

func OrElse added in v0.6.2

func OrElse[T comdef.XintOrFloat](val, defVal T) T

OrElse return default value on val is zero, else return val

func OutRange added in v0.6.9

func OutRange[T comdef.IntOrFloat](val, min, max T) bool

OutRange check if val not in int/float range [min, max]

func Percent

func Percent(val, total int) float64

Percent returns a values percent of the total

func QuietFloat added in v0.5.3

func QuietFloat(in any) float64

QuietFloat convert value to float64, will ignore error. alias of SafeFloat

func QuietInt added in v0.5.3

func QuietInt(in any) int

QuietInt convert value to int, will ignore error

func QuietInt64 added in v0.5.3

func QuietInt64(in any) int64

QuietInt64 convert value to int64, will ignore error

func QuietString added in v0.5.3

func QuietString(val any) string

QuietString convert intX/floatX value to string, other type convert by fmt.Sprint

func QuietUint added in v0.5.3

func QuietUint(in any) uint

QuietUint convert any to uint, will ignore error

func QuietUint64 added in v0.6.15

func QuietUint64(in any) uint64

QuietUint64 convert any to uint64, will ignore error

func RandInt added in v0.5.0

func RandInt(min, max int) int

RandInt alias of RandomInt()

func RandIntWithSeed added in v0.5.0

func RandIntWithSeed(min, max int, seed int64) int

RandIntWithSeed alias of RandomIntWithSeed()

func RandomInt added in v0.3.6

func RandomInt(min, max int) int

RandomInt return a random int at the [min, max)

Usage:

RandomInt(10, 99)
RandomInt(100, 999)
RandomInt(1000, 9999)

func RandomIntWithSeed added in v0.5.0

func RandomIntWithSeed(min, max int, seed int64) int

RandomIntWithSeed return a random int at the [min, max)

Usage:

seed := time.Now().UnixNano()
RandomIntWithSeed(1000, 9999, seed)

func SafeFloat added in v0.6.12

func SafeFloat(in any) float64

SafeFloat convert value to float64, will ignore error

func SafeInt added in v0.6.11

func SafeInt(in any) int

SafeInt convert value to int, will ignore error

func SafeInt64 added in v0.6.7

func SafeInt64(in any) int64

SafeInt64 convert value to int64, will ignore error

func SafeString added in v0.6.12

func SafeString(val any) string

SafeString convert intX/floatX value to string, other type convert by fmt.Sprint

func SafeUint added in v0.6.11

func SafeUint(in any) uint

SafeUint convert any to uint, will ignore error

func SafeUint64 added in v0.6.15

func SafeUint64(in any) uint64

SafeUint64 convert any to uint64, will ignore error

func StrInt added in v0.5.11

func StrInt(s string) int

StrInt convert.

func StrIntOr added in v0.6.12

func StrIntOr(s string, defVal int) int

StrIntOr convert string to int, return default val on failed

func String added in v0.5.0

func String(val any) string

String convert intX/floatX value to string, other type convert by fmt.Sprint

func StringOr added in v0.6.12

func StringOr(val any, defVal string) string

StringOr convert intX/floatX value to string, will return default value on error

func StringOrDefault added in v0.6.12

func StringOrDefault(val any, defVal string) string

StringOrDefault convert intX/floatX value to string, will return default value on error

func StringOrErr added in v0.5.3

func StringOrErr(val any) (string, error)

StringOrErr convert intX/floatX value to string, return error on failed

func StringOrPanic added in v0.5.0

func StringOrPanic(val any) string

StringOrPanic convert intX/floatX value to string, will panic on error

func SwapMax added in v0.6.2

func SwapMax[T comdef.XintOrFloat](x, y T) (T, T)

SwapMax compare and always return [max, min] value

func SwapMaxI64 added in v0.5.3

func SwapMaxI64(x, y int64) (int64, int64)

SwapMaxI64 compare and return max, min value

func SwapMaxInt added in v0.5.3

func SwapMaxInt(x, y int) (int, int)

SwapMaxInt compare and return max, min value

func SwapMin added in v0.6.8

func SwapMin[T comdef.XintOrFloat](x, y T) (T, T)

SwapMin compare and always return [min, max] value

func ToFloat added in v0.2.2

func ToFloat(in any) (float64, error)

ToFloat convert value to float64, return error on failed

func ToFloatWith added in v0.6.15

func ToFloatWith(in any, optFns ...ConvOptionFn[float64]) (f64 float64, err error)

ToFloatWith try to convert value to float64. can with some option func, more see ConvOption.

func ToInt added in v0.2.2

func ToInt(in any) (int, error)

ToInt convert value to int, return error on failed

func ToInt64 added in v0.2.2

func ToInt64(in any) (int64, error)

ToInt64 convert value to int64, return error on failed

func ToInt64With added in v0.6.15

func ToInt64With(in any, optFns ...ConvOptionFn[int64]) (i64 int64, err error)

ToInt64With try to convert value to int64. can with some option func, more see ConvOption.

func ToIntWith added in v0.6.15

func ToIntWith(in any, optFns ...ConvOptionFn[int]) (iVal int, err error)

ToIntWith convert value to int, can with some option func.

Example:

ToIntWithFunc(val, mathutil.WithNilAsFail, mathutil.WithUserConvFn(func(in any) (int, error) {
})

func ToString added in v0.5.0

func ToString(val any) (string, error)

ToString convert intX/floatX value to string, return error on failed

func ToStringWith added in v0.6.15

func ToStringWith(in any, optFns ...comfunc.ConvOptionFn) (string, error)

ToStringWith try to convert value to string. can with some option func, more see comfunc.ConvOption.

func ToUint added in v0.2.2

func ToUint(in any) (u64 uint, err error)

ToUint convert value to uint, return error on failed

func ToUint64 added in v0.6.15

func ToUint64(in any) (uint64, error)

ToUint64 convert value to uint64, return error on failed

func ToUint64With added in v0.6.15

func ToUint64With(in any, optFns ...ConvOptionFn[uint64]) (u64 uint64, err error)

ToUint64With try to convert value to uint64. can with some option func, more see ConvOption.

func ToUintWith added in v0.6.15

func ToUintWith(in any, optFns ...ConvOptionFn[uint]) (uVal uint, err error)

ToUintWith try to convert value to uint. can with some option func, more see ConvOption.

func TryToString added in v0.5.0

func TryToString(val any, defaultAsErr bool) (string, error)

TryToString try convert intX/floatX value to string

if defaultAsErr is False, will use fmt.Sprint convert other type

func Uint added in v0.2.2

func Uint(in any) (uint, error)

Uint convert any to uint, return error on failed

func Uint64 added in v0.6.15

func Uint64(in any) (uint64, error)

Uint64 convert any to uint64, return error on failed

func Uint64Or added in v0.6.15

func Uint64Or(in any, defVal uint64) uint64

Uint64Or convert any to uint64, return default val on failed

func Uint64OrDefault added in v0.6.15

func Uint64OrDefault(in any, defVal uint64) uint64

Uint64OrDefault convert any to uint64, return default val on failed

func Uint64OrErr added in v0.6.15

func Uint64OrErr(in any) (uint64, error)

Uint64OrErr convert value to uint64, return error on failed

func UintOr added in v0.6.12

func UintOr(in any, defVal uint) uint

UintOr convert any to uint, return default val on failed

func UintOrDefault added in v0.6.12

func UintOrDefault(in any, defVal uint) uint

UintOrDefault convert any to uint, return default val on failed

func UintOrErr added in v0.5.3

func UintOrErr(in any) (uint, error)

UintOrErr convert value to uint, return error on failed

func WithHandlePtr added in v0.6.15

func WithHandlePtr[T any](opt *ConvOption[T])

WithHandlePtr set ConvOption.HandlePtr option

func WithNilAsFail added in v0.6.15

func WithNilAsFail[T any](opt *ConvOption[T])

WithNilAsFail set ConvOption.NilAsFail option

Example:

ToIntWithFunc(val, mathutil.WithNilAsFail[int])

func ZeroOr added in v0.6.12

func ZeroOr[T comdef.XintOrFloat](val, defVal T) T

ZeroOr return default value on val is zero, else return val

Types

type ConvOption added in v0.6.15

type ConvOption[T any] struct {
	// if ture: value is nil, will return convert error;
	// if false(default): value is nil, will convert to zero value
	NilAsFail bool
	// HandlePtr auto convert ptr type(int,float,string) value. eg: *int to int
	// 	- if true: will use real type try convert. default is false
	//	- NOTE: current T type's ptr is default support.
	HandlePtr bool
	// set custom fallback convert func for not supported type.
	UserConvFn ToTypeFunc[T]
}

ConvOption convert options

func NewConvOption added in v0.6.15

func NewConvOption[T any](optFns ...ConvOptionFn[T]) *ConvOption[T]

NewConvOption create a new ConvOption

func (*ConvOption[T]) WithOption added in v0.6.15

func (opt *ConvOption[T]) WithOption(optFns ...ConvOptionFn[T])

WithOption set convert option

type ConvOptionFn added in v0.6.15

type ConvOptionFn[T any] func(opt *ConvOption[T])

ConvOptionFn convert option func

func WithUserConvFn added in v0.6.15

func WithUserConvFn[T any](fn ToTypeFunc[T]) ConvOptionFn[T]

WithUserConvFn set ConvOption.UserConvFn option

type ToFloatFunc added in v0.6.12

type ToFloatFunc func(any) (float64, error)

ToFloatFunc convert value to float

type ToInt64Func added in v0.6.12

type ToInt64Func func(any) (int64, error)

ToInt64Func convert value to int64

type ToIntFunc added in v0.6.12

type ToIntFunc func(any) (int, error)

ToIntFunc convert value to int

type ToTypeFunc added in v0.6.15

type ToTypeFunc[T any] func(any) (T, error)

ToTypeFunc convert value to defined type

type ToUint64Func added in v0.6.15

type ToUint64Func func(any) (uint64, error)

ToUint64Func convert value to uint

type ToUintFunc added in v0.6.12

type ToUintFunc func(any) (uint, error)

ToUintFunc convert value to uint

Jump to

Keyboard shortcuts

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