mirror of
https://github.com/duanhf2012/origin.git
synced 2026-05-13 02:17:35 +08:00
新增通用math函数
This commit is contained in:
37
util/math/math.go
Normal file
37
util/math/math.go
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
package math
|
||||||
|
|
||||||
|
type NumberType interface {
|
||||||
|
int | int8 | int16 | int32 | int64 | float32 | float64 | uint | uint8 | uint16 | uint32 | uint64
|
||||||
|
}
|
||||||
|
|
||||||
|
type SignedNumberType interface {
|
||||||
|
int | int8 | int16 | int32 | int64 | float32 | float64
|
||||||
|
}
|
||||||
|
|
||||||
|
type FloatType interface {
|
||||||
|
float32 | float64
|
||||||
|
}
|
||||||
|
|
||||||
|
func Max[NumType NumberType](number1 NumType, number2 NumType) NumType {
|
||||||
|
if number1 > number2 {
|
||||||
|
return number1
|
||||||
|
}
|
||||||
|
|
||||||
|
return number2
|
||||||
|
}
|
||||||
|
|
||||||
|
func Min[NumType NumberType](number1 NumType, number2 NumType) NumType {
|
||||||
|
if number1 < number2 {
|
||||||
|
return number1
|
||||||
|
}
|
||||||
|
|
||||||
|
return number2
|
||||||
|
}
|
||||||
|
|
||||||
|
func Abs[NumType SignedNumberType](Num NumType) NumType {
|
||||||
|
if Num < 0 {
|
||||||
|
return -1 * Num
|
||||||
|
}
|
||||||
|
|
||||||
|
return Num
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user