maths

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2023 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	DefaultTolerance = 0.0001 // 默认误差范围
)

Variables

This section is empty.

Functions

func Clamp

func Clamp[V generic.Number](value, min, max V) V

Clamp 将给定值限制在最小值和最大值之间

func Compare

func Compare[V generic.Ordered](a V, expression CompareExpression, b V) bool

Compare 根据特定表达式比较两个值

func GetDefaultTolerance

func GetDefaultTolerance() float64

GetDefaultTolerance 获取默认误差范围

func IsContinuity added in v0.0.3

func IsContinuity[V generic.Integer](values []V) bool

IsContinuity 检查一组值是否连续

Example
package main

import (
	"fmt"
	"github.com/kercylan98/minotaur/utils/maths"
)

func main() {
	fmt.Println(maths.IsContinuity([]int{1, 2, 3, 4, 5, 6, 7}))
	fmt.Println(maths.IsContinuity([]int{1, 2, 3, 5, 5, 6, 7}))
}
Output:

true
false

func IsContinuityWithSort added in v0.0.3

func IsContinuityWithSort[V generic.Integer](values []V) bool

IsContinuityWithSort 检查一组值排序后是否连续

func Max

func Max[V generic.Number](a, b V) V

Max 返回两个数之中较大的值

func MaxMin

func MaxMin[V generic.Number](a, b V) (max, min V)

MaxMin 将两个数按照较大的和较小的顺序进行返回

func Merge

func Merge[V generic.SignedNumber](refer, a, b V) V

Merge 通过一个参考值合并两个数字

func Min

func Min[V generic.Number](a, b V) V

Min 返回两个数之中较小的值

func MinMax

func MinMax[V generic.Number](a, b V) (min, max V)

MinMax 将两个数按照较小的和较大的顺序进行返回

func Pow

func Pow(a, n int) int

Pow 整数幂运算

func PowInt64

func PowInt64(a, n int64) int64

PowInt64 整数幂运算

func ToContinuous added in v0.0.3

func ToContinuous[V generic.Integer](nums []V) map[V]V

ToContinuous 将一组非连续的数字转换为从1开始的连续数字

  • 返回值是一个 map,key 是从 1 开始的连续数字,value 是原始数字
Example
package main

import (
	"fmt"
	"github.com/kercylan98/minotaur/utils/maths"
)

func main() {
	var nums = []int{1, 2, 3, 5, 6, 7, 9, 10, 11}
	var continuous = maths.ToContinuous(nums)

	fmt.Println(nums)
	fmt.Println(continuous)

}
Output:

[1 2 3 5 6 7 9 10 11]
map[1:1 2:2 3:3 4:5 5:6 6:7 7:9 8:10 9:11]

func Tolerance

func Tolerance[V generic.Number](value1, value2, tolerance V) bool

Tolerance 检查两个值是否在一个误差范围内

func UnMerge

func UnMerge[V generic.SignedNumber](refer, num V) (a, b V)

UnMerge 通过一个参考值取消合并的两个数字

Types

type CompareExpression

type CompareExpression int

CompareExpression 比较表达式

const (
	CompareGreaterThan        CompareExpression = 1 // 大于
	CompareGreaterThanOrEqual CompareExpression = 2 // 大于等于
	CompareLessThan           CompareExpression = 3 // 小于
	CompareLessThanOrEqual    CompareExpression = 4 // 小于等于
	CompareEqual              CompareExpression = 5 // 等于
)

Jump to

Keyboard shortcuts

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