mathKit

package
v2.9.114 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Round 四舍五入,保留n位小数
	/*
	   e.g.
	   	fmt.Println(Round(3.14, 1))  // 3.1
	   	fmt.Println(Round(3.15, 1))  // 3.2
	   	fmt.Println(Round(-3.14, 1)) // -3.1
	   	fmt.Println(Round(-3.15, 1)) // -3.2
	*/
	Round = mathutil.RoundToFloat

	// RoundToString 四舍五入,保留n位小数,返回字符串
	RoundToString = mathutil.RoundToString

	// 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
	*/
	TruncRound func(x float64, n int) float64 = mathutil.TruncRound
)
View Source
var (
	// Exponent 指数计算(x的n次方).
	/*
		@param x 底数
		@param n 指数
		@return x^n

		e.g.
		(2, 10)	=> 1024
		(8, 2)	=> 64
	*/
	Exponent func(x, n int64) int64 = mathutil.Exponent

	// Factorial 阶乘.
	/*
		在数学中,阶乘是一种运算符号,表示所有小于等于该数的正整数的积12345。
		例如,5的阶乘表示为5!,其值为1×2×3×4×5=12012345。此外,0的阶乘被定义为112345。

		e.g.
		fmt.Println(mathKit.Factorial(0)) // 1
		fmt.Println(mathKit.Factorial(1)) // 1(=1)
		fmt.Println(mathKit.Factorial(5)) // 120(=1*2*3*4*5)
	*/
	Factorial func(x uint) uint = mathutil.Factorial

	// IsPrime 判断质数.
	IsPrime func(n int) bool = mathutil.IsPrime

	// Fibonacci 计算斐波那契数列的第n个数.
	Fibonacci func(first, second, n int) int = mathutil.Fibonacci

	// PointDistance 计算两个坐标点的距离.
	PointDistance func(x1, y1, x2, y2 float64) float64 = mathutil.PointDistance

	// AngleToRadian 将角度值转为弧度值.
	AngleToRadian func(angle float64) float64 = mathutil.AngleToRadian

	// RadianToAngle 将弧度值转为角度值.
	RadianToAngle func(radian float64) float64 = mathutil.RadianToAngle

	// Sin 正弦函数(计算弧度的正弦值).
	Sin func(radian float64, precision ...int) float64 = mathutil.Sin

	// Cos 余弦函数(计算弧度的余弦值).
	Cos func(radian float64, precision ...int) float64 = mathutil.Cos

	// Log 计算以base为底n的对数.
	Log func(n, base float64) float64 = mathutil.Log
)
View Source
var (
	Inf func(sign int) float64 = math.Inf

	IsInf func(f float64, sign int) bool = math.IsInf
)
View Source
var (
	NaN func() float64 = math.NaN

	IsNaN func(f float64) (is bool) = math.IsNaN
)

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 计算平均数(可能需要对结果调用RoundToFloat方法四舍五入)

func Ceil

func Ceil(f float64, places int) float64

Ceil 向上取整,类似于 math.Ceil(),但功能更强大.

PS: (1) NOTE: this will panic on NaN, +/-inf (2) 个人感觉: 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 int) float64

Floor 向下取整,类似于 math.Floor(),但功能更强大.

PS: (1) NOTE: this will panic on NaN, +/-inf (2) 个人感觉: x轴向左.

e.g.

(3.14, 1)	=> 3.1
(-3.14, 1)	=> -3.2

func GCD added in v2.8.152

func GCD[T constraints.Integer](integers ...T) T

GCD 计算最大公约数。

func LCM added in v2.8.152

func LCM[T constraints.Integer](integers ...T) T

LCM 计算最小公倍数。

func Max added in v2.1.32

func Max[T constraints.Ordered](p T, args ...T) (max T)

Max 获取最大值.

PS: Go1.21即以上,建议使用内置函数 max().

func MaxBy added in v2.1.48

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

func Min added in v2.1.32

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

Min 获取最小值.

PS: Go1.21即以上,建议使用内置函数 min().

func MinBy added in v2.1.48

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

func Percent added in v2.8.152

func Percent(val, total float64, places int32) float64

Percent 计算百分比,保留 places 位小数

@param places 保留的小数位 @return (val * 100 / total)

e.g.

fmt.Println(mathKit.Percent(1, 3, 3)) // 33.333

func Sum

Sum 求和.

PS: 不使用lancet的mathutil,原因: 泛型更广.

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 求和.

PS: 不使用lancet的mathutil,原因: 泛型更广、没有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