Documentation ¶
Index ¶
- Variables
- func Abs[T constraints.Integer | constraints.Float](x T) T
- func Average[T constraints.Integer | constraints.Float](numbers ...T) T
- func Ceil(f float64, places int32) float64
- func Clamp[T constraints.Ordered](value T, min T, max T) T
- func Floor(f float64, places int32) float64
- func Max[T constraints.Ordered](p T, args ...T) (max T)deprecated
- func MaxBy[T any](slice []T, comparator func(T, T) bool) T
- func Min[T constraints.Ordered](p T, args ...T) (min T)deprecated
- func MinBy[T any](slice []T, comparator func(T, T) bool) T
- func Round(f float64, places int32) float64
- func Sum[T constraints.Float | constraints.Integer | constraints.Complex](s []T) T
- func SumBy[T any, R constraints.Float | constraints.Integer | constraints.Complex](s []T, iteratee func(item T) R) R
- func Ternary[T any](flag bool, trueRst, falseRst T) T
Constants ¶
This section is empty.
Variables ¶
var Exponent func(x, n int64) int64 = mathutil.Exponent
Exponent 指数.
@return x^n
e.g.
rst := mathKit.Exponent(2, 10) fmt.Println(rst) // 1024
var TruncRound func(x float64, n int) float64 = mathutil.TruncRound
TruncRound 截断n位小数(不进行四舍五入)
@param n 保留的小数位(可以 < 0,但有点奇怪!!!)
e.g. (1234.124, 0) => 1234 (1234.124, -1) => 1234 (1234.124, -2) => 0
(100.125, 2) => 100.12 (100.125, 3) => 100.125
Functions ¶
func Average ¶ added in v2.1.48
func Average[T constraints.Integer | constraints.Float](numbers ...T) T
Average 计算平均数.
PS: 可能需要对返回值进行四舍五入.
func Ceil ¶
Ceil 向上取整,类似于 math.Ceil(),但功能更强大.
PS: 个人感觉: x轴向右.
e.g. (3.14, 1) => 3.2 (-3.14, 1) => -3.1
func Clamp ¶
func Clamp[T constraints.Ordered](value T, min T, max T) T
Clamp clamps number within the inclusive lower and upper bounds.
case value < min: 返回min case value > max: 返回max case others: 返回value
e.g. (0, -10, 10) => 0 (-42, -10, 10) => -10 (42, -10, 10) => 10
func Floor ¶
Floor 向下取整,类似于 math.Floor(),但功能更强大.
PS: (1) 个人感觉: x轴向左.
e.g. (3.14, 1) => 3.1 (-3.14, 1) => -3.2
func Max
deprecated
added in
v2.1.32
func Min
deprecated
added in
v2.1.32
func Round ¶
Round 保留小数位(四舍五入),类似于 math.Round(),但功能更强大
PS: (1) 参考:https://zhuanlan.zhihu.com/p/152050239?from_voters_page=true (2) 个人感觉:先把正负号拿出来 => 进行取舍 => 把正负号还回去 (3) 传参为负数的情况下,Golang的四舍五入与别的语言(Java、JavaScript)不同,详情见"Golang.docx"中的"math标准库".
@param places 小数位数(如果最后几个都是0的话,会省略掉);可以为负值
e.g. (3.14, 1) => 3.1 (3.15, 1) => 3.2 (-3.14, 1) => -3.1 (-3.15, 1) => -3.2
(3.1001, 2) => 3.1 (521, -1) => 520
func Sum ¶
func Sum[T constraints.Float | constraints.Integer | constraints.Complex](s []T) T
Sum 求和.
e.g. ([]int{0, 1, 2, 3}) => 6
func SumBy ¶
func SumBy[T any, R constraints.Float | constraints.Integer | constraints.Complex](s []T, iteratee func(item T) R) R
SumBy 求和.
@param s 可以为nil(此时返回0) @param iteratee (1) 不能为nil(除非s == nil),否则会导致panic: runtime error: invalid memory address or nil pointer dereference
(2) 传参为T类型,返回值为R类型
e.g.
i := mathKit.SumBy[string, int]([]string{"0", "1", "2"}, func(item string) int { tmp, _ := strconv.Atoi(item) return tmp }) fmt.Println(i) // 3
func Ternary ¶ added in v2.2.39
Ternary 三目运算符(ternary operator)
PS: (1) Golang原生不支持三目运算符,因此只能自己实现 (2) ternary adj.三元的,三重的 (3) 简单的情况可以使用此函数,但复杂情况(比如第2、3个传参需要动态计算)请自行处理
参考: golang三目运算的实现 https://www.cnblogs.com/GetcharZp/p/15172602.html
@param rst0 true条件下的返回值 @param rst1 false条件下的返回值
Types ¶
This section is empty.