mathKit

package
v2.1.17 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2023 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Ceil

func Ceil(x float64) float64

Ceil 向上取整

PS: 个人感觉,向右取整数.

e.g. (3.6) => 4 (-3.6) => -3

func Clamp

func Clamp[T constraints.Ordered](value T, min T, max T) T

Clamp

value < min: 返回min value > max: 返回max others: 返回value

e.g. (0, -10, 10) => 0 (-42, -10, 10) => -10 (42, -10, 10) => 10

func Floor

func Floor(x float64) float64

Floor 向下取整

PS: 个人感觉,向左取整数.

e.g. (3.6) => 3 (-3.6) => -4

func Round

func Round(x float64) float64

Round 四舍五入

TODO: 看后续版本Golang math.Round() 的返回值是否与别的语言一致.

!!!: (1) 此方法与"Java(Math.round)"和"JavaScript(Math.round)"一致; (2) Golang 1.17.7原生的math.Round()与"Java(Math.round())"和"JavaScript(Math.round())"不一致,传参为负数的情况.

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

Types

This section is empty.

Jump to

Keyboard shortcuts

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