package mathutilimport// IsNumeric returns true if the given character is a numeric, otherwise false.func ( byte) bool { return >= '0' && <= '9' }// IsInteger strict check the given value is an integer(intX,uintX), otherwise false.func ( any) bool {switch .(type) {caseint, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, uintptr:returntruedefault:returnfalse }}// IsFloat returns true if the given character is a float(32/64), otherwise false.func ( any) bool {switch .(type) {casefloat32, float64:returntruedefault:returnfalse }}// 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, ">") // truefunc (, any, string) bool {if == nil || == nil {returnfalse }switch fVal := .(type) {casefloat64:if , := ToFloat(); == nil {returnCompFloat(, , ) }casefloat32:if , := ToFloat(); == nil {returnCompFloat(float64(), , ) }default: // as int64if , := ToInt64(); == nil {if , := ToInt64(); == nil {returnCompInt64(, , ) } } }returnfalse}// CompInt compare all intX,uintX type value. returns `first op(=,!=,<,<=,>,>=) second`func [ comdef.Xint](, , string) ( bool) {returnCompValue(, , )}// CompInt64 compare int64 value. returns `first op(=,!=,<,<=,>,>=) second`func (, int64, string) bool {returnCompValue(, , )}// CompFloat compare float64,float32 value. returns `first op(=,!=,<,<=,>,>=) second`func [ comdef.Float](, , string) ( bool) {returnCompValue(, , )}// CompValue compare intX,uintX,floatX value. returns `first op(=,!=,<,<=,>,>=) second`func [ comdef.Number](, , string) ( bool) {switch {case"<", "lt": = < case"<=", "lte": = <= case">", "gt": = > case">=", "gte": = >= case"=", "eq": = == case"!=", "ne", "neq": = != }return}// InRange check if val in int/float range [min, max]func [ comdef.Number](, , ) bool {return >= && <= }// OutRange check if val not in int/float range [min, max]func [ comdef.Number](, , ) bool {return < || > }// InUintRange check if val in unit range [min, max]func [ comdef.Uint](, , ) bool {if == 0 {return >= }return >= && <= }// InDelta Check whether two floating-point numbers are equal within a specified margin of error//// Params:// want - 期望的浮点数值// give - 实际给定的浮点数值// delta - 允许的误差范围func [ comdef.Float](, , float64) bool { := float64() - float64()if < 0 { = - }return <= }// InDeltaAny Check whether two floating-point numbers are equal within a specified margin of errorfunc (, any, float64) bool { , := ToFloat()if != nil {returnfalse } , := ToFloat()if != nil {returnfalse }returnInDelta(, , )}
The pages are generated with Goldsv0.8.4. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.