mathKit

package
v2.2.49 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Exponent func(x, n int64) int64 = mathutil.Exponent

Exponent 指数.

@return x^n

e.g.

rst := mathKit.Exponent(2, 10)
fmt.Println(rst) // 1024
View Source
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 Abs added in v2.1.48

func Abs[T constraints.Integer | constraints.Float](x T) T

Abs 绝对值.

func Average added in v2.1.48

func Average[T constraints.Integer | constraints.Float](numbers ...T) T

Average 计算平均数.

PS: 可能需要对返回值进行四舍五入.

func Ceil

func Ceil(f float64, places int32) float64

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

func Floor(f float64, places int32) float64

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 Max[T constraints.Ordered](p T, args ...T) (max T)

Max

Deprecated: Go1.21使用内置函数 max().

func MaxBy added in v2.1.48

func MaxBy[T any](slice []T, comparator func(T, T) bool) T

func Min deprecated added in v2.1.32

func Min[T constraints.Ordered](p T, args ...T) (min T)

Min

Deprecated: Go1.21使用内置函数 min().

func MinBy added in v2.1.48

func MinBy[T any](slice []T, comparator func(T, T) bool) T

func Round

func Round(f float64, places int32) float64

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

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

func Ternary[T any](flag bool, trueRst, falseRst T) T

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.

Jump to

Keyboard shortcuts

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