Documentation
¶
Overview ¶
* @Author: kamalyes 501893067@qq.com * @Date: 2024-11-09 00:50:58 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2024-11-12 22:09:26 * @FilePath: \go-toolbox\pkg\mathx\number.go * @Description: * * Copyright (c) 2024 by kamalyes, All Rights Reserved.
* @Author: kamalyes 501893067@qq.com * @Date: 2024-11-09 00:50:58 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2024-11-10 00:15:15 * @FilePath: \go-toolbox\pkg\mathx\proba.go * @Description: * 此文件定义了一个Proba结构体和相关的方法,用于根据给定的概率判断是否为真。 * * Copyright (c) 2024 by kamalyes, All Rights Reserved.
* @Author: kamalyes 501893067@qq.com * @Date: 2024-11-09 00:50:58 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2024-11-10 15:19:08 * @FilePath: \go-toolbox\pkg\mathx\unstable.go * @Description: * * Copyright (c) 2024 by kamalyes, All Rights Reserved.
Index ¶
- func AtLeast[T Numerical](x, lower T) T
- func AtMost[T Numerical](x, upper T) T
- func Between[T Numerical](x, lower, upper T) T
- func CalcEntropy(m map[interface{}]int) float64
- func CountPathSegments(path string, prefixes ...string) int
- func EqualSlices[T comparable](a, b []T) bool
- func LongestCommonPrefix(a, b string) int
- func MinMax(list []interface{}, f MinMaxFunc) (interface{}, error)
- func ZeroValue[T any]() T
- type MinMaxFunc
- type Numerical
- type Proba
- type Unstable
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AtLeast ¶
func AtLeast[T Numerical](x, lower T) T
AtLeast 返回 x 和 lower 中的较大值。 参数: x - 要比较的第一个数值 lower - 要比较的第二个数值(下限) 返回值: 返回 x 和 lower 中的较大值。
func AtMost ¶
func AtMost[T Numerical](x, upper T) T
AtMost 返回 x 和 upper 中的较小值。 参数: x - 要比较的第一个数值 upper - 要比较的第二个数值(上限) 返回值: 返回 x 和 upper 中的较小值。
func Between ¶
func Between[T Numerical](x, lower, upper T) T
Between 将 x 的值限制在 [lower, upper] 范围内。 如果 x 小于 lower,则返回 lower; 如果 x 大于 upper,则返回 upper; 否则,返回 x 本身。 参数: x - 要限制的数值 lower - 范围的下限 upper - 范围的上限 返回值: 返回 x 被限制在 [lower, upper] 范围内的值。
func CalcEntropy ¶
CalcEntropy 计算给定概率分布(map)的熵值 熵值表示信息的不确定性或随机性
func CountPathSegments ¶
CountPathSegments 计算路径中指定前缀的参数数量,默认为 ":" 和 "*"。
func EqualSlices ¶ added in v0.11.7
func EqualSlices[T comparable](a, b []T) bool
EqualSlices 比较两个切片是否相等,支持任意类型
func LongestCommonPrefix ¶
LongestCommonPrefix 返回两个字符串的最长公共前缀的长度。
func MinMax ¶
func MinMax(list []interface{}, f MinMaxFunc) (interface{}, error)
MinMax 是一个通用的函数,用于计算列表中元素的最小值或最大值。 它接收一个interface{}类型的切片和一个MinMaxFunc类型的函数, 根据提供的函数决定是计算最小值还是最大值。 如果列表为空,则返回错误。
Types ¶
type MinMaxFunc ¶
type MinMaxFunc func(a, b interface{}) interface{}
MinMaxFunc 是用于计算最小值或最大值的函数类型,接收两个interface{}类型的参数,返回一个interface{}类型的结果。
type Numerical ¶
type Numerical interface { ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~float32 | ~float64 }
Numerical 是一个接口,表示一系列数值类型,包括有符号和无符号的整数以及浮点数。
type Proba ¶
type Proba struct {
// contains filtered or unexported fields
}
Proba结构体用于根据给定的概率判断事件是否为真。 它内部使用了一个线程不安全的随机数生成器,并通过互斥锁来保证并发安全。
func (*Proba) TrueOnProba ¶
TrueOnProba方法用于根据给定的概率判断事件是否为真。 它接收一个浮点数作为概率参数(范围应在0到1之间),并返回一个布尔值。 如果生成的随机数小于给定的概率,则返回true;否则返回false。 此方法通过加锁来保证在并发访问时的线程安全。
type Unstable ¶
type Unstable struct {
// contains filtered or unexported fields
}
Unstable 结构体用于基于给定的偏差值生成围绕均值附近的随机值。
func (Unstable) AroundDuration ¶
AroundDuration 根据给定的基础时长和偏差值返回一个随机的时长。